C # File Download Method,

Source: Internet
Author: User

C # File Download Method,

C # File Download Method

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO; public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){ } // Download TransmitFileprotected void Button1_Click(object sender, EventArgs e){Response.ContentType = "application/x-zip-compressed";Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");string filename = Server.MapPath("DownLoad/z.zip");Response.TransmitFile(filename);} // Download WriteFileprotected void Button2_Click(object sender, EventArgs e){string fileName ="asd.txt";// File name saved by the clientstring filePath=Server.MapPath("DownLoad/aaa.txt");// Path FileInfo fileInfo = new FileInfo(filePath);Response.Clear();Response.ClearContent();Response.ClearHeaders();Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);Response.AddHeader("Content-Length", fileInfo.Length.ToString());Response.AddHeader("Content-Transfer-Encoding", "binary");Response.ContentType = "application/octet-stream";Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");Response.WriteFile(fileInfo.FullName);Response.Flush();Response.End();} // WriteFile multipart downloadprotected void Button3_Click(object sender, EventArgs e){string fileName = "aaa.txt";// File name saved by the clientstring filePath = Server.MapPath("DownLoad/aaa.txt");// Path System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true){const long ChunkSize = 102400;// 100 K read the file and only 100 K each time, which can relieve the pressure on the serverbyte[] buffer = new byte[ChunkSize]; Response.Clear();System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);long dataLengthToRead = iStream.Length;// Obtain the total size of the downloaded fileResponse.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));// Read sizeResponse.OutputStream.Write(buffer, 0, lengthRead);Response.Flush();dataLengthToRead = dataLengthToRead - lengthRead;}Response.Close();}} // Stream downloadprotected void Button4_Click(object sender, EventArgs e){string fileName = "aaa.txt";// File name saved by the clientstring filePath = Server.MapPath("DownLoad/aaa.txt");// Path // Download an object in the form of a streamingFileStream 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";// Notify the browser to download the file instead of opening itResponse.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));Response.BinaryWrite(bytes);Response.Flush();Response.End(); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.