NET is available to download (download statistics included):

Download downloads while recording, you can not use hyperlinks direct download, but should jump to the middle of a aspx page with hyperlinks and CS files in this page's Page_Load method, using Response.Redirect ( "url"); points to download document. After this click download, the customer sees is always the download page, will not see the middle of the page.

1, <a href="mp3/生命的希望不再失落.mp3"> download </a>

2, Response.Redirect ( "hope mp3 / life is no longer lost .mp3");

3, Server.Transfer ( "hope mp3 / life is no longer lost .mp3");

4, flow ways: online following four methods

// TransmitFile Realization protected void the Button1_Click ( Object SENDER, EventArgs E) { / * Microsoft Response object provides a new method to solve TransmitFile use Response.BinaryWrite lead Aspnet_wp.exe process recycling when downloading files over 400mb of success can not be Download the issue. Code is as follows: * / Response.ContentType = " file application / X-ZIP-Compressed " ; the Response.AddHeader ( " the Content-Disposition " , " Attachment; filename = z.zip " ); String filename = the Server.MapPath ( " DownLoad / z.zip " );










Response.TransmitFile(filename);
}

// WriteFile实现下载
protected void Button2_Click( object sender, EventArgs e)
{
/*
using System.IO;

*/
string fileName = " asd.txt " ; // 客户端保存的文件名
string filePath = Server.MapPath( " DownLoad/aaa.txt " ); // 路径

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分块下载
protected void Button3_Click( object sender, EventArgs e)
{
string fileName = " aaa.txt " ; // 客户端保存的文件名
string filePath = Server.MapPath( " DownLoad/aaa.txt " ); // 路径

System.IO.FileInfo fileInfo
= new System.IO.FileInfo(filePath);

if (fileInfo.Exists == true )
{
Const Long ChunkSize = 102400 ; // 100K each read file, read only 100K, which can ease the pressure on the server byte [] Buffer = new new byte [ChunkSize]; Response.Clear (); System.IO.FileStream iStream = System.IO.File.OpenRead (filePath); Long dataLengthToRead = iStream.Length; // get the total size of the downloaded file Response.ContentType = " the 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)); // 读取的大小
Response.OutputStream.Write(buffer, 0 , lengthRead);
Response.Flush();
dataLengthToRead
= dataLengthToRead - lengthRead;
}
Response.Close();
}
}

// 流方式下载
protected void The Button4_Click ( Object SENDER, EventArgs E)
{
String fileName = " aaa.txt " ; // client stored file name String filePath = the Server.MapPath ( " DownLoad / aaa.txt " ); // path // character stream download file form the FileStream FS = new new the FileStream (filePath, FileMode.Open); byte [] bytes = new new byte [( int ) fs.Length]; fs.Read (bytes, 0 , bytes.Length); fs.Close ( );







Response.ContentType
= " application/octet-stream " ;
// 通知浏览器下载文件而不是打开
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 " );
  
// '文件名称
  WebForm.Response.AddHeader( " content-disposition " , " attachment; filename=' " + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) + " ' " );
  WebForm.Response.ContentType
= " File application / OCTET-Stream " ;
  
// 'file content   WebForm.Response.Write (FileBody); // ----------- WebForm.Response.End (); } // above code is download a text file dynamically generated, if the file already exists in the server route entity, the following function can be: public void DownloadFileByFilePath (System.Web.UI.Page WebForm1, FileNameWhenUserDownload String, String FilePath) {   WebForm.Response. clearHeaders ();   WebForm.Response.Clear ();   WebForm.Response.Expires = 0 ; WebForm.Response.Buffer = to true ;   WebForm.Response.AddHeader ( " the Accept-Language " ,













" zh-tw " );
  
// 文件名称
  WebForm.Response.AddHeader( " content-disposition " , " attachment; filename=' " + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) + " ' " );
  WebForm.Response.ContentType
= " Application/octet-stream " ;
  
// 文件内容
  WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath)); // ---------
  WebForm.Response.End();
}

Reproduced in: https: //www.cnblogs.com/Eleanore/archive/2011/12/22/2298373.html

Guess you like

Origin blog.csdn.net/weixin_34289454/article/details/94606218