2012-09-11 129 views
1

我在將文件傳輸到瀏覽器時收到錯誤,但僅在第二次到第N次出現錯誤。它第一次工作正常。第二次傳輸文件時出錯

C:\Users\gfinzer\AppData\Local\Temp\BTnADAkZ.pdf. could not be saved because the source file could not be read 

下面是代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Get the parameters 
    string reportName = Utils.ParseStringRequest(Request, "reportName") ?? string.Empty; 
    string reportGuid = Session["reportGuid"].ToString(); 
    string path = Path.Combine(ReportPath(), Utils.GetSessionReportName(reportName, reportGuid)); 

    using (var fileStream = File.Open(path, FileMode.Open)) 
    { 
     Response.ClearHeaders(); 
     Response.Clear(); 
     Response.ContentType = "application/octet-stream"; 
     Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\""); 
     Response.AddHeader("Content-Length", fileStream.Length.ToString(CultureInfo.InvariantCulture)); 
     StreamHelper.CopyStream(fileStream, Response.OutputStream); 
     Response.Flush(); 
    } 

} 

這裏是CopyStream:

public static void CopyStream(Stream input, Stream output) 
    { 
     byte[] buffer = new byte[32768]; 
     int read; 
     while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      output.Write(buffer, 0, read); 
     } 
    } 
+3

究竟是「第二次」?回傳之後? –

+0

'StreamHelper.CopyStream'如何工作?你能發佈代碼嗎? – Xharze

+0

是報告書生成。這是我第二次創建報告。我已經添加了CopyStream。 –

回答

1

我可能是錯的,但你需要的FlushResponse.End()電話嗎?

+0

就是這樣。非常感謝。 –

+0

沒問題!這是一個猜測tbh! – simonlchilds

相關問題