TransmitFile implementation Download
protected void Button1_Click (object sender, EventArgs e)
{
/*
Microsoft has provided a new method for response objects TransmitFile to solve the use of Response.BinaryWrite
A problem that caused the Aspnet_wp.exe process to be recycled and could not be downloaded successfully when downloading more than 400MB files.
The code is as follows:
*/
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader ("Content-disposition", "Attachment;filename=z.zip");
string filename = Server.MapPath ("Download/z.zip");
Response.TransmitFile (filename);
}
WriteFile Implementation Download
protected void button2_click (object sender, EventArgs e)
{
/*
Using System.IO;
*/
string filename = "Asd.txt";//client saved file name
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-Block Download
protected void Button3_Click (object sender, EventArgs e)
{
string filename = "Aaa.txt";//client saved file name
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 Read the file every time, read only 100K, which can ease the pressure on 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 ();
}
}
Streaming mode download
protected void Button4_Click (object sender, EventArgs e)
{
string filename = "Aaa.txt";//client saved file name
String filePath = Server.MapPath ("Download/aaa.txt");//path
//download files as character streams
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";
//Notify browser to download file instead of open
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ httputility.urlencode (FileName, System.Text.Encoding.UTF8));
response.binarywrite (bytes);
Response.Flush ();
Response.End ();
}
//----------------------------------------------------------
public void DownloadFile (System.Web.UI.Page webform,string filenamewhenuserdownload, String filebody)
{
WebForm.Response.ClearHeaders ();
WebForm.Response.Clear ();
WebForm.Response.Expires = 0;
WebForm.Response.Buffer = true;
WebForm.Response.AddHeader ("Accept-language", "ZH-TW");
' File name
WebForm.Response.AddHeader ("Content-disposition", "attachment; Filename= ' "+system.web.httputility.urlencode (Filenamewhenuserdownload, System.Text.Encoding.UTF8) +" "");
WebForm.Response.ContentType = "Application/octet-stream";
' File contents
WebForm.Response.Write (filebody);//-----------
WebForm.Response.End ();
}
The above code is to download a dynamically generated text file, if the file already exists on the server side of the entity path, you can use the following function:
public void Downloadfilebyfilepath (System.Web.UI.Page webform,string filenamewhenuserdownload, String FilePath)
{
WebForm.Response.ClearHeaders ();
WebForm.Response.Clear ();
WebForm.Response.Expires = 0;
WebForm.Response.Buffer = true;
WebForm.Response.AddHeader ("Accept-language", "ZH-TW");
File name
WebForm.Response.AddHeader ("Content-disposition", "attachment; Filename= ' "+ System.Web.HttpUtility.UrlEncode (filenamewhenuserdownload, System.Text.Encoding.UTF8) +" "");
WebForm.Response.ContentType = "Application/octet-stream";
File contents
WebForm.Response.Write (System.IO.File.ReadAllBytes (FilePath));//---------
WebForm.Response.End ();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--Save the document
String DocName, docextended;
Stream doc = fudoc.filecontent;
int doclength = FuDoc.PostedFile.ContentLength;
byte[] Docdata = new Byte[doclength];
Doc. Read (docdata, 0, doclength);
docextended = fuDoc.FileName.Substring (FuDoc.FileName.IndexOf ("."));
if (string. IsNullOrEmpty (Tbdoc.text))
DocName = Fudoc.filename;
Else
DocName = Tbdoc.text + docextended;
Safetydocinfo data = new Safetydocinfo (Ddlsort.text, DocName, FuDoc.PostedFile.ContentType, Docdata);
Safetydoc safety = new Safetydoc ();
Safety. Insert (data);
Tbdoc.text = string. Empty;
--Open Document
public void Viewsafetydoc (string pdocsort, String pdocname)
{
oracleparameter[] parms = Getsafetydocparm (Sql_view_safetydoc);
Parms[0]. Value = Pdocsort;
PARMS[1]. Value = Pdocname;
using (OracleDataReader RDR = Oraclehelper.executereader (oraclehelper.connectionstring, CommandType.Text, SQL_View_ Safetydoc, Parms))
{
while (RDR. Read ())
{
HttpContext.Current.Response.Clear ();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312");
HttpContext.Current.Response.ContentType = rdr. GetString (1);
HttpContext.Current.Response.BinaryWrite ((byte[]) rdr["Doc");
String FileName = rdr. GetString (0);
filename = System.Web.HttpUtility.UrlEncode (filename, System.Text.Encoding.UTF8);
HttpContext.Current.Response.AppendHeader ("Content-disposition", "attachment;filename=" + filename);
}
}
}