Asp.Net–下載檔案

來源:互聯網
上載者:User

實現方式1:

    protected void DownLoad_Click(object sender, EventArgs e)    {        //擷取要下載的檔案        string filename = Server.MapPath("~/upload/電腦科學與技術.rar");       FileInfo f = new FileInfo(filename);        //設定檔案頭        Response.ContentType = "application/zip";        //對檔案名稱進行編碼處理,並設定儲存時的預設檔案名稱        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(f.Name, System.Text.Encoding.UTF8));        //輸出檔案        Response.TransmitFile(filename);            }

效果:

實現方式2:

    protected void DownLoad_Click(object sender, EventArgs e)    {        //擷取要下載的檔案         string file = Server.MapPath("~/upload/DesignPattern.rar");        FileInfo f = new FileInfo(file);        //清空緩衝區內容         Response.Clear();        //設定檔案頭         Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlDecode(f.Name,System.Text.Encoding.UTF8));        Response.AddHeader("Content-Length", f.Length.ToString());        Response.AddHeader("Content-Transfer-Encoding", "binary");        Response.ContentType = "application/zip";        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");        Response.WriteFile(f.FullName);        Response.End();    }
下載中文名檔案時存在問題.
效果:
 
實現方式3:
    protected void DownLoad_Click(object sender, EventArgs e)    {        //擷取要下載的檔案,並將資料讀入數組         string filepath = Server.MapPath("~/upload/DesignPattern.rar");        FileInfo fi = new FileInfo(filepath);        FileStream fs = new FileStream(filepath, FileMode.Open);        int filelength = (int)fs.Length;        byte[] bt = new byte[filelength];        fs.Read(bt, 0, filelength);        fs.Close();        //設定檔案頭及儲存時的檔案名稱         Response.ContentType = "application/zip";        Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlDecode(fi.Name,System.Text.Encoding.UTF8));        Response.AddHeader("Content-Length", filelength.ToString());        //輸出檔案         Response.BinaryWrite(bt);        Response.Flush();        Response.End();    }
下載中文名檔案時存在問題.
效果:
 
實現方式4:
    protected void DownLoad_Click(object sender, EventArgs e)    {        string filepath = Server.MapPath("~/upload/DesignPattern.rar");        FileStream fs = new FileStream(filepath, FileMode.Open);        int filelength = (int)fs.Length;        byte[] b = new byte[filelength];        fs.Read(b, 0, filelength);        fs.Close();        Response.ContentType = "application/zip";        Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");        Response.AddHeader("Content-Length", filelength.ToString());        Response.OutputStream.Write(b, 0, filelength);        Response.OutputStream.Close();        Response.Flush();        Response.Close();    }

實現方式5:


    protected void DownLoad_Click(object sender, EventArgs e)    {        //鑾峰彇瑕佷笅杞界殑鏂囦歡,騫跺皢鏁版嵁璿誨靉鏁扮粍        string filepath = Server.MapPath("~/upload/aspnetmvc-stepbystep.rar");        FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);        long filelength = fs.Length;        int readsize = 102400;        byte[] b = new byte[readsize];          Response.ContentType = "application/octet-stream";        Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");        Response.AddHeader("Content-Length", filelength.ToString());        while (filelength > 0 && Response.IsClientConnected)        {            int receive = fs.Read(b, 0, readsize);            Response.OutputStream.Write(b, 0, receive);            Response.Flush();            b = new byte[readsize];            filelength = filelength - receive;        }        fs.Close();        Response.OutputStream.Close();        Response.Close();    }
 
注意:
Response.AppendHeader("content-disposition", "attachment;filename=" + filename);//附件下載Response.AppendHeader("content-disposition", "online;filename=" + filename);//線上開啟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.