Copy codeThe Code is as follows: // TransmitFile for download
Protected void button#click (object sender, EventArgs e)
{
/*
Microsoft provides a new TransmitFile method for the Response object to solve the problem of using Response. BinaryWrite
When a file with more than Mbit/s is downloaded, The aspnet_wp.exe process cannot be recycled.
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 );
}
// Download WriteFile
Protected void Button2_Click (object sender, EventArgs e)
{
/*
Using System. IO;
*/
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 multipart 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; // 100 K reads only 100 K each time, which can relieve 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; // obtain 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 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 an object in the form of a streaming
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 the browser to download the 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 ();
}
//----------------------------------------------------------
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 content
WebForm. Response. Write (FileBody );//-----------
WebForm. Response. End ();
}
// The above code downloads a dynamically generated text file. If the file already exists in the server-side object 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 Content
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 the 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 );
}
}
}