2013-04-21 91 views
0

我正在向客戶端發送文件以供下載。文件可能很大(高達幾GB),所以我以大塊的形式發送它。這是我的代碼:System.ObjectDisposedException:無法訪問C#asp.net中的已關閉文件4

//code to get data in resp stream. 
     using (Stream inputStream = resp.GetResponseStream()) 
     { 
      if (System.IO.File.Exists(TargetFile)) 
      { 
       System.IO.File.Delete(TargetFile); 
      } 
      using (FileStream fs = System.IO.File.Open(TargetFile, FileMode.Create, FileAccess.ReadWrite)) 
      { 
       byte[] buffer = new byte[SegmentSize]; 
       int bytesRead; 
       while ((bytesRead = inputStream.Read(buffer, 0, SegmentSize)) > 0) 
       { 
        fs.Write(buffer,0,bytesRead); 
       } 
       Response.AddHeader("Content-Disposition", "Attachment;filename=targetFileName.pdf"); 
       return File(fs, "application/pdf"); 
      } 
     } 

當我點擊鏈接下載它給了我上面的錯誤。我嘗試着不返回任何東西,在這種情況下,它會下載文件,但文件的大小爲零。錯誤在最後一行。

+0

退房http://stackoverflow.com/questions/1584785/using-and-stream – 2013-08-21 23:40:25

回答

0

我在將上傳的文件附加到電子郵件時遇到同樣的問題。添加下面的web.config工作:

<system.web> 
    <httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/> 
</system.web> 
相關問題