博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET文件的上传下载提交分页
阅读量:5878 次
发布时间:2019-06-19

本文共 4323 字,大约阅读时间需要 14 分钟。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DownLoadPage.aspx.cs" Inherits="WebApplication2.DownLoadPage" %>

下载压缩文件

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" %>
这里放商品信息
用户名:
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" %>

 
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("上传成功");        }    }}

 

 

 

转载于:https://www.cnblogs.com/xiaz/p/5242983.html

你可能感兴趣的文章
bzoj1821
查看>>
文法分析
查看>>
记那次失败了的面试
查看>>
程序包+创建包规范+创建包体+删除程序包
查看>>
Vue过渡效果之JS过渡
查看>>
3-继承
查看>>
java中如何实现类似goto的作法
查看>>
Switch入门第二讲
查看>>
海归千千万 为何再无钱学森
查看>>
vue2.0 仿手机新闻站(六)详情页制作
查看>>
FreeRTOS的内存管理
查看>>
JSP----九大内置对象
查看>>
mysql存储引擎简析
查看>>
Linux下干净卸载mysql详解
查看>>
LINUX信号量实现对公共数据的读写
查看>>
acpi和btrfs-安装opensuse时的选项
查看>>
The Z-Index CSS Property: A Comprehensive Look | Smashing Coding
查看>>
Java中HashMap详解
查看>>
Office版本差别引发的语法问题
查看>>
Apache——访问控制
查看>>