2016-04-27 23 views
0

我將PDF文件放置在不同的(FILE-Server)服務器計算機上,並且託管了我的MVC應用程序的IIS計算機有權訪問該文件服務器。從IIS機器,我可以通過以下URI訪問該文件:通過來自不同服務器的頭文件下載文件

file://file-server/data-folder/pdf/19450205.pdf 

我想使我的MVC應用程序的用戶可以通過點擊下載鏈接或按鈕來下載他們相應的文件。所以可能我不得不爲該鏈接/按鈕編寫一些操作。

我試着在下面的方式來使用文件返回類型爲我的行動方法:

public ActionResult FileDownload() 
{ 
    string filePth = @"file://file-server/data-folder/pdf/19450205.pdf"; 
    return File(filePth , "application/pdf"); 
} 

,但上面的代碼給URI的例外,不支持。

我也嘗試使用FileStream讀取數組中的字節返回字節往下載,但FileStream也給出了錯誤的不適當的「虛擬路徑」,因爲文件不放在虛擬路徑內,它在單獨的服務器上。

回答

0
public ActionResult Download() 
{ 
    var document = = @"file://file-server/data-folder/pdf/19450205.pdf"; 
    var cd = new System.Net.Mime.ContentDisposition 
    { 
     // for example foo.bak 
     FileName = document.FileName, 

     // always prompt the user for downloading, set to true if you want 
     // the browser to try to show the file inline 
     Inline = false, 
    }; 
    Response.AppendHeader("Content-Disposition", cd.ToString()); 
    return File(document.Data, document.ContentType); 
} 
0

感謝您的回覆,但兩項建議都無效。

作爲文件需要通過URI訪問,使用FileInfo給出錯誤:不支持URI格式。

我設法得到這種通過以下機制來完成:

public ActionResult FaxFileDownload() 
    { 
     string filePth = @"file://file-server/data-folder/pdf/19450205.pdf"; 

     WebClient wc = new WebClient(); 
     Stream s = wc.OpenRead(filePth); 

     return File(s, "application/pdf"); 

    } 

感謝所有。

相關問題