<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DownLoadPage.aspx.cs" Inherits="WebApplication2.DownLoadPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <p> <a href="/IMBA_3.86b_ai.zip">下载压缩文件</a> <br /> </p> <p> <asp:Button ID="Button1" runat="server" Text="下载" onclick="Button1_Click" /> </p> <p> </p> </asp:Content>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace WebApplication2 { public partial class DownLoadPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //下载 protected void Button1_Click(object sender, EventArgs e) { //用代码将文件发送给浏览器 //可以先检测用户的权限或积分再做文件的发送..... //指定我接下来给你发送的是什么类型文件 Response.ContentType = "application/zip"; //指定头信息,告诉浏览器是一个附件 Response.AddHeader("content-disposition","attchment;filename=aaa.zip"); //1.一次将文件放到内存中,在发送的 //Response.WriteFile("/IMBA_3.86b_ai.zip"); //2.用流变读取文件边发送 FileStream fs = new FileStream(Server.MapPath("/IMBA_3.86b_ai.zip"), FileMode.Open, FileAccess.Read, FileShare.Read); Stream st= Response.OutputStream; //将fs内容读取写入到st byte[] buffer = new byte[102400]; while (true) { int len = fs.Read(buffer, 0, buffer.Length); if (len == 0) break; st.Write(buffer,0,len); Response.Flush(); } fs.Close(); Response.End(); } } }
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Goods.aspx.cs" Inherits="WebApplication2.Goods" %> <%@ Register src="NotNullTextBox.ascx" tagname="NotNullTextBox" tagprefix="uc1" %> <%@ Register assembly="ServerControl1" namespace="ServerControl1" tagprefix="cc1" %> <%@ Register src="PageApart.ascx" tagname="PageApart" tagprefix="uc2" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 这里放商品信息<br /> 用户名:<uc1:NotNullTextBox ID="NotNullTextBox1" runat="server" ErrorMsg="用户名不能为空!!!" /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="提交" /> <br /> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <br /> <br /> <uc2:PageApart ID="PageApart1" runat="server" Fileds="*" Gv="" PageIndex="1" PageSize="20" PrimaryKey="pid" Table="product" /> </asp:Content>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication2 { public partial class Goods : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //提交 protected void Button2_Click(object sender, EventArgs e) { //NotNullTextBox nb = new NotNullTextBox(); //TextBox c = NotNullTextBox1.FindControl("TextBox1") as TextBox;//找到输入框 //Response.Write(c.Text); string x = NotNullTextBox1.Text; Response.Write(x);//利用属性的封装 } } }
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="UpLoadPage.aspx.cs" Inherits="WebApplication2.UpLoadPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <p> <br /> <asp:FileUpload ID="FileUpload1" runat="server" /> </p> <p> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" /> </p> <p> </p> </asp:Content>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace WebApplication2 { public partial class UpLoadPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //上传 protected void Button1_Click(object sender, EventArgs e) { //准备文件名 string fileName = "UpLoad/"; string type = Path.GetExtension(FileUpload1.FileName); fileName += DateTime.Now.Ticks + type; FileUpload1.SaveAs(Server.MapPath(fileName)); //1.将上传文件缓存到内存,在将内存一次性放到磁盘(耗内存) FileUpload1.SaveAs(Server.MapPath(fileName)); //2.用流的方式,边上传边接受,接受到的内容写入到磁盘文件中 FileStream fs = new FileStream(Server.MapPath(fileName),FileMode.Create,FileAccess.Write,FileShare.Read); HttpPostedFile hpf = FileUpload1.PostedFile; Stream st = hpf.InputStream;//上传的文件的文件流 //st 读,读取到的写入到fs里 byte[] buffer = new byte[102400]; while (true) { int len = st.Read(buffer,0,buffer.Length); if (len == 0) break; fs.Write(buffer,0,len); } st.Close(); fs.Close(); Response.Write("上传成功"); } } }
原文:http://www.cnblogs.com/xiaz/p/5242983.html