. net file Download method rollup

Source: Internet
Author: User



Reprinted from: http://blog.sina.com.cn/s/blog_680942070101ahsq.html



Test pass, compatible with IE, Google:


stringPath = Server.MapPath ("~/upload"+ Bill. AttachmentPath + Bill. AttachmentName);//Attachment Save Path if(file.exists (path)) {//download a file as a stream of charactersFileStream fs =NewFileStream (path, FileMode.Open); byte[] bytes =New byte[(int) fs.                Length]; Fs. Read (Bytes,0, Bytes.                Length); Fs.                Close (); Response.ContentType="Application/octet-stream"; //notifies the browser to download a file instead of opening it stringfilename =Bill.                Realname; if(Request.UserAgent.IndexOf ("MSIE") >-1) {filename=System.Web.HttpUtility.UrlEncode (filename, System.Text.Encoding.UTF8); } Response.AddHeader ("content-disposition","attachment; Filename="+httputility.urlencode (filename, System.Text.Encoding.UTF8));                Response.BinaryWrite (bytes);                Response.Flush ();            Response.End (); }


TransmitFile implementation Download



protected void Button1_click1 (object sender, EventArgs e)



{






String strFileName = "Three idle device management system operating manual iems.ppt";



Response.ContentType = "application/x-zip-compressed";



response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");



string filename = BLL. Config.part_em_upload_doc + strFileName;



Bll. Config.part_em_upload_doc as Path ("d:/emuploaddoc/")



Response.AddHeader ("Content-disposition", "Attachment;filename=" +server.urlpathencode (strFileName));



server.urlpathencode () resolves the file name garbled problem.






Response.TransmitFile (filename);



}//writefile Implementation Download



protected void button2_click (object sender, EventArgs e)



{






string filename = "Asd.txt";//file name saved by the client



String 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 Sub-block download



protected void Button3_Click (object sender, EventArgs e)



{



string filename = "Aaa.txt";//file name saved by the client



String filePath = Server.MapPath ("Download/aaa.txt");//path



System.IO.FileInfo FileInfo = new System.IO.FileInfo (FilePath);



if (fileinfo.exists = = True)



{



Const LONG ChunkSize = 102400;//100k reads the file each time, only reads 100K, this can alleviate the pressure of the server



byte[] buffer = new Byte[chunksize];



Response.Clear ();



System.IO.FileStream IStream = System.IO.File.OpenRead (FilePath);



Long Datalengthtoread = istream.length;//Gets the total size of the downloaded file



Response.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 size



Response.OutputStream.Write (buffer, 0, lengthread);



Response.Flush ();



Datalengthtoread = Datalengthtoread-lengthread;



}



Response.close ();



}



}



Stream mode Download



protected void Button4_Click (object sender, EventArgs e)



{



string filename = "Aaa.txt";//file name saved by the client



String filePath = Server.MapPath ("Download/aaa.txt");//path



Download a file as a stream of characters



FileStream 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";



Notifies the browser to download a file instead of opening it



Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));



Response.BinaryWrite (bytes);



Response.Flush ();



Response.End ();



}



C#:
///
File download
///
///
private void FileDownload (string fullfilename)
{
FileInfo DownloadFile = new FileInfo (fullfilename);
Response.Clear ();
Response.ClearHeaders ();
Response.buffer=false;
Response.contenttype= "Application/octet-stream";
Response.appendheader ("Content-disposition", "Attachment;filename=" +httputility.urlencode (DownloadFile.FullName , System.Text.Encoding.UTF8));
Response.appendheader ("Content-length", DownloadFile.Length.ToString ());
Response.WriteFile (Downloadfile.fullname);
Response.Flush ();
Response.End ();
}


File read/write
Public Stream GetFileStream (string filename)
{//File address
Uri Fileuri = new uri (filename, urikind.relative);
Save
StreamResourceInfo info = new StreamResourceInfo (this.stream, NULL);
if (This.stream is System.IO.FileStream)
{
This.stream.Seek (0, Seekorigin.begin);
}
StreamResourceInfo stream = System.Windows.Application.GetResourceStream (info, Fileuri);
if (stream! = null)
{
return stream. Stream;
}
return null;
}
Public IEnumerable Getfilenamesinzip ()
{
BinaryReader reader = new BinaryReader (stream);
Stream. Seek (0, Seekorigin.begin);
string name = NULL;
List names = new list ();
while (Parsefileheader (reader, out name))
{
Names. ADD (name);
}
return names;
}
private static bool Parsefileheader (BinaryReader Reader, out string filename)
{
filename = null;
if (reader. Basestream.position < reader. Basestream.length)
{
filename = null;
if (reader. Basestream.position < reader. Basestream.length)
{
Reader. Basestream.seek (2, seekorigin.current);
Short Genpurposeflag = reader. ReadInt16 ();
if ((((((int) genpurposeflag) & 0x08)! = 0))
return false;
Reader. Basestream.seek (ten, seekorigin.current);
int compressedsize = reader. ReadInt32 ();
int uncompressedsize = reader. ReadInt32 ();
Short filenamelenght = reader. ReadInt16 ();
Short extrafieldlenght = reader. ReadInt16 ();
filename = new string (reader. ReadChars (filenamelenght));
if (string. IsNullOrEmpty (filename))
return false;
Reader.                    Basestream.seek (Extrafieldlenght + compressedsize, seekorigin.current); if (uncompressedsize = = 0) return Parsefileheader (reader, out filename);
Else
return true;
}
}
return false;
}


. net file Download method rollup


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.