以块的形式下载文件

时间:2011-11-25 08:52:01

标签: c# asp.net

我遇到的问题如下: 有一些SOAP Web服务允许读取流文件。我需要读取分成块的整个文件并传输给用户。所有操作都不应阻止UI主线程:用户按下保存文件对话框中的“保存”按钮,并能够转到下一页或执行其他操作。我将非常感谢样品解决方案。请注意,该解决方案应该适用于IIS 5.1。

此致 麦

3 个答案:

答案 0 :(得分:2)

将ASP.NET中的文件逐个字节地下载到响应页面。在msdn处查看:

try
   {
      System.String filename = "C:\\downloadJSP\\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=\"myFile.txt\"";
      Response.AppendHeader("Content-Disposition", disHeader);

      // transfer the file byte-by-byte to the response object
      System.IO.FileInfo fileToDownload = new
         System.IO.FileInfo(filename);
      System.IO.FileStream fileInputStream = new
        System.IO.FileStream(fileToDownload.FullName,
        System.IO.FileMode.Open, System.IO.FileAccess.Read);
      int i;
      while ((i = fileInputStream.ReadByte()) != - 1)
      {
         Response.Write((char)i);
      }
      fileInputStream.Close();
      Response.Flush();
      Response.Close();
   }
   catch (System.Exception e)
   // file IO errors
   {
      SupportClass.WriteStackTrace(e, Console.Error);
   }

有些文章可以帮助您实施和解决错误:

download an excel file from byte() on https server

asp.net downloading file from ftp getting it as byte[] then saving it as file

Remote file Download via ASP.NET corrupted file

Response.WriteFile cannot download a large file

答案 1 :(得分:0)

ProcessRequest方法表单下载器HttpHandler:

public void ProcessRequest(HttpContext context)
        {
            RequestTarget target = RequestTarget.ParseFromQueryString(context.Request.QueryString);
            Guid requestId = new Guid(context.Request.QueryString["requestId"]);
            string itemName = HttpUtility.UrlDecode(context.Request.QueryString["itemName"]);
            if (target != null &&
                !requestId.Equals(Guid.Empty) &&
                !string.IsNullOrEmpty(itemName))
            {
                HttpResponse response = context.Response;
                response.Buffer = false;
                response.Clear();
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + itemName + "\"");
                response.ContentType = "application/octet-stream";
                int length = 100000, i = 0;
                byte[] fileBytes;
                do
                {
                    fileBytes = WS.ReadFile(requestId, target, i * length, length);
                    i++;
                    response.OutputStream.Write(fileBytes, 0, fileBytes.Length);
                    response.Flush();
                }
                while (fileBytes != null && fileBytes.Length == length);
            }
        }

整个问题不是组织下载操作,而是满足下载操作不应阻止UI主线程的条件:用户按下'保存'保存文件对话框上的按钮,可以移动到下一页或执行其他操作。 Niranjan Kala编写的解决方案导致,当文件非常大时,用户在下载操作完成之前无法查看其他页面。我很感激,但这不是我的意思......

答案 2 :(得分:0)

如果我理解正确,您希望让浏览器在不重新加载当前页面的情况下发起对该文件的新请求。最简单的方法可能是创建一个target =“_ blank”的链接。这样的事情应该做:

<a href="http://yourhost/yourdownloadhandler?requestid=blah&itemName=blahblah" target="_blank">Download file</a>

如果您提供的内容类型为application/octet-stream,则大多数浏览器会将文件保存到磁盘。