且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在我的网站上使用下载功能

更新时间:2023-12-02 15:09:46

以下是使用ASP .NET下载文件的示例代码.
Here is the sample code to download a file using ASP .NET

try
{
   System.String filename = "myFile.txt";

   // set the http content type to "APPLICATION/OCTET-STREAM
   Response.ContentType = "APPLICATION/OCTET-STREAM";
   
   // initialize the http content-disposition header to
   // indicate a file attachment with the default filename
   // "myFile.txt"
   System.String disHeader = "Attachment; Filename=\"" + filename +
      "\"";
   Response.AppendHeader("Content-Disposition", disHeader);

   // transfer the file byte-by-byte to the response object
   System.IO.FileInfo fileToDownload = new
      System.IO.FileInfo("C:\\download\\myFile.txt");
   Response.Flush();
   Response.WriteFile(fileToDownload.FullName);}
catch (System.Exception e)
// file IO errors
{
   SupportClass.WriteStackTrace(e, Console.Error);
}