2015-09-19 17 views
-1

我想知道如何提示用戶下載pdf文件。如何將本地pdf文件返回到瀏覽器以保存或使用asp.net打開mvc

我有下面的代碼,但它沒有返回任何東西。

public ActionResult DownloadAssetClassGuide() 
{ 
    string folder = @"C:\NewFolder"; 
    string file = "xyz.pdf"; 

    string fullPath = Path.Combine(folder, file); 

    byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath); 

    return File(fileBytes, "application/pdf", file); 
} 

我錯過了什麼?

+0

你需要指定'內容Disposition'頭。欲瞭解更多信息,請參閱我標記爲重複的帖子。 – Dai

+0

你如何將用戶發送到操作方法? –

回答

0

因爲您正在使用虛擬路徑,所以文件或文件夾的任何路徑應首先映射到服務器。嘗試映射首先使用 HttpContext.Current.Server.MapPath的路徑到服務器:

FileInfo fInfo = new FileInfo(Server.MapPath(fullPath)); 
FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); 
BinaryReader reader = new BinaryReader(fileStream); 
byte[] fileBytes = reader.ReadBytes((int)fInfo.Length); 

這種方法與我工作時,我面臨同樣的問題

相關問題