zoukankan      html  css  js  c++  java
  • 文件下载代码

    找到一个简单的文件下载代码,好好记下

    原网址:http://blog.csdn.net/littlehb/archive/2007/04/20/1572440.aspx

    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();
            }
  • 相关阅读:
    我的openwrt学习笔记(四):OpenWrt源码下载
    算法系列之“汉若塔”
    尖峰在线Oracle OCM实战 --开创国内Dtrace先河!
    Android开发屏幕适配知识点
    【cocos2d-js官方文档】十九、Cocos2d-JS单文件引擎使用指引
    PHP重载
    作为一个在城市打拼的人。
    关于马云最帅的照片是哪一张?!你们感受下!哈哈哈哈!(10P)
    Banana PI (香蕉派) 安装 ubuntu-core-14 最小核心的操作步骤
    (转载)偏序集的Dilworth定理学习
  • 原文地址:https://www.cnblogs.com/Anlycp/p/1285107.html
Copyright © 2011-2022 走看看