• 更有效的文件下载功能


    1.普通下载:

            //下载文件的路径
            string path=Server.MapPath("广告.jpg");
            //下载文件的名称
            string filename = "广告.jpg";
            System.IO.FileInfo toDownload = new System.IO.FileInfo(path);
            Response.Clear();
            if (System.IO.Path.GetExtension(filename) == ".jpg")
            ...{
                Response.AddHeader("Content-Disposition", "attachment;filename=NEW_" + HttpUtility.UrlEncode(toDownload.Name));
                Response.ContentType = "application/x-zip-compressed";
                Response.TransmitFile(path);
                Response.End();
            }

    2.大文件分成小块下载:

            //下载文件的路径
            string path = Server.MapPath("广告.jpg");
            //下载文件的名称
            string filename = "广告.jpg";
            System.IO.FileInfo toDownload = new System.IO.FileInfo(path);
    
            if (toDownload.Exists == true)
            {
                const long ChunkSize = 10000;
                byte[] buffer = new byte[ChunkSize];
    
                Response.Clear();
                System.IO.FileStream iStream = System.IO.File.OpenRead(path);
                long dataLengthToRead = iStream.Length;
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=new_" + HttpUtility.UrlEncode(toDownload.Name));
                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();
            }
  • 相关阅读:
    发呆发呆发呆发呆发呆发呆发
    大众捷达看想吃 v 觉得分开才相聚离开都出现
    yjggj
    test4
    test3
    test2
    test1
    Java并发之线程池ThreadPoolExecutor源码分析学习
    Java并发之AQS同步器学习
    ThreadLocal和ThreadLocalMap源码分析
  • 原文地址:https://www.cnblogs.com/52net/p/2522448.html
Copyright © 2020-2023  润新知