zoukankan      html  css  js  c++  java
  • C#文件下载两种方式

    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();
            }

    Kyle

  • 相关阅读:
    CSS font 复合属性的顺序
    HTML元素的offsetWidht、clientWidth、scrollWidth属性区别
    javascript document.compatMode属性
    IE下Array.prototype.slice.call(params,0)
    JavaScript中的apply()方法和call()方法使用介绍
    字符串与数组转换
    好的网站链接
    [转]走向视网膜(Retina)的Web时代
    如何对一个公司的供应链管理进行数据分析?
    Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
  • 原文地址:https://www.cnblogs.com/lovewife/p/1428811.html
Copyright © 2011-2022 走看看