• HTML上传文件支持大文件上传,下载


    上传

    1.修改配置文件web.config,<system.webServer>下面加入

    <security>

    <requestFiltering >

    <requestLimits maxAllowedContentLength="4096000000" ></requestLimits>

    </requestFiltering>

    </security>

     

     2.form中加入enctype ="multipart/form-data"

    <form action="" method="" enctype ="multipart/form-data">

     

    3.action响应处理中加入  HttpPostedFileBase file =Request.Files[0];就可以获取到页面传来的文件流了,然后进行处理。

    注意,如果不加入1所示配置项,上传文件不能支持大文件。
    下载

     

    public string DownLoad(){

                //客户端保存的文件名

                string fileName = Request.Params["filename"];

                //路径 人为加入了路径前缀,正式版需要修正

                string filePath =Server.MapPath(Request.ApplicationPath+"UploadFiles/UserUpload/" + fileName);

                //分段流的形式下载文件

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

                if (fileInfo.Exists == true)

                {

                    const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力

                    byte[] buffer = new byte[ChunkSize];

                    Response.Clear();

                    System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);

                    long dataLengthToRead = iStream.Length;//获取下载的文件总大小

                    Response.ContentType = "application/octet-stream";

                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));

                    while (dataLengthToRead > 0 && Response.IsClientConnected)

                    {

                        int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小

                        Response.OutputStream.Write(buffer, 0, lengthRead);

                        Response.Flush();

                        dataLengthToRead = dataLengthToRead - lengthRead;

                    }

                    Response.Close();

                }

                return "已下载";

            }

     

    分段传输的方式实现下载,缓解服务器压力。

    如果不采取分段处理,直接用二进制节传输,会出现报错内存不足的情况。

    如下列子:

     string fileName = "CodeShark.zip";//客户端保存的文件名

        string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路径

        //以字符流的形式下载文件

        FileStream fs = new FileStream(filePath, FileMode.Open);

            byte[] bytes = new byte[(int)fs.Length];

            fs.Read(bytes, 0, bytes.Length);

            fs.Close();

            Response.ContentType = "application/octet-stream";

            //通知浏览器下载文件而不是打开

        Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

            Response.BinaryWrite(bytes);

            Response.Flush();

            Response.End();

     

    demo下载地址:https://dwz.cn/fgXtRtnu

     

  • 相关阅读:
    Debugging Auto Layout:Ambiguous Layouts
    Debugging Auto Layout:Unsatisfiable Layouts
    Debugging Auto Layout
    Auto Layout Cookbook:Views with Intrinsic Content Size
    编译地址与运行地址
    Memory Controller
    ARM寄存器
    C++指针悬挂(赋值运算符重载)
    多态性,友元与静态成员 基础知识小结
    ARM 汇编指令集
  • 原文地址:https://www.cnblogs.com/xproer/p/10740992.html
Copyright © 2020-2023  润新知