2014-07-06 89 views
1

我試圖將文件保存到用戶電腦,但我無法做到這一點。 此方法提供錯誤,因爲正試圖將文件保存在C的某處。我明白這並不合適。我想將文件保存在下載文件夾中或通過文件對話框提示用戶。ASP.net下載並保存用戶文件夾中的文件

void SaveReport(Telerik.Reporting.Report report, string fileName) 
{ 
    ReportProcessor reportProcessor = new ReportProcessor(); 
    Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource(); 
    instanceReportSource.ReportDocument = report; 
    RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null); 

    using (FileStream fs = new FileStream(fileName, FileMode.Create)) 
    { 
     fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length); 
    } 
} 

如何將文件保存在用戶機器的下載文件夾中或提示filedialog以允許用戶選擇目的地?

+1

filestream將寫入服務器上的文件而不是客戶端的機器上。嘗試將文檔字節寫入響應輸出流和內容處置http頭以響應。 –

回答

1

您無法強制將文件下載到的位置。

在您的代碼中,您不應寫入文件,而是從響應中寫入OutputStream

RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null); 

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"file.pdf\""); 
HttpContext.Current.Response.OutputStream.Write(result.DocumentBytes, 0, result.DocumentBytes.Length); 
+0

發生了一些不同的事情,但該文件沒有下載。它出現在瀏覽器中完全搞砸了。 – Nullbyte

+0

非常感謝支持。我真的很感激額外的幫助。它工作正常。 – Nullbyte

0

文件流將寫入服務器上的文件而不是客戶機上的文件。

嘗試將文檔字節寫入響應輸出流,然後將content-disposition http標頭寫入響應。

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

這將提示用戶基於瀏覽器的設置文件下載。

您無法控制客戶機中的哪個目錄文件進入。

+0

我正在嘗試使用您的代碼。我如何將它集成到我的代碼中?對不起,但我真的是一個初學者自學。 – Nullbyte

相關問題