[英]IIS VirtualDirectories, and accessing their contents from a ASP.NET MVC Project
由於 Chromium 不再允許指向本地文件的鏈接,我正在嘗試測試幾個解決方案,以允許用戶在本地網絡共享上打開 PDF,而無需先下載它們(當前的解決方法)。
我測試了一個純 JavaScript 解決方案,這個解決方案效果很好。
但是,我正在嘗試在 IIS 中使用虛擬目錄,該目錄指向包含用戶可以訪問的文件的網絡共享。
我創建了一個測試應用程序並將其發布在我的本地機器上。
下面是我創建的虛擬目錄的屏幕截圖。
[HttpPost] public ActionResult OpenPDF() { string directory = "./pdf"; string file = "/light.pdf"; byte[] fileBytes = System.IO.File.ReadAllBytes(directory + file); return File(fileBytes, "application/pdf"); }
我假設虛擬目錄位於根目錄中。 我試圖找到一些在代碼中訪問虛擬目錄的示例,但我沒有找到。
我訪問的資源:
https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/application/virtualdirectory
https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/create-virtual-directory-folder-remote-computer#:~:text=In%20the%20Internet%20Information%20Services,和%20then%20click%20Virtual%20Directory 。
虛擬目錄導航到 http://localhost/MyWebsite 而不是 http://localhost:8080
asp.net 中文件路徑(URL)上的./、../、../../、~/ 之間的區別
https://www.c-sharpcorner.com/UploadFile/francissvk/create-virtual-directory-in-iis/
任何和所有的幫助將不勝感激,謝謝。
在 ASP.NET 內核中,您可以返回VirtualFileResult
一個 FileResult,它在執行時使用主機提供的機制將使用虛擬路徑指定的文件寫入響應。
或者您可以執行Server.MapPath("~/pdf/light.pdf")
來獲取虛擬路徑。
返回與指定虛擬路徑對應的物理文件路徑。
在您的示例代碼中,您可以這樣使用它:
[HttpPost]
public ActionResult OpenPDF()
{
string directory = "/pdf"; // notice I removed the .
string file = "/light.pdf";
var filepath = Server.MapPath("~" + directory + file);
byte[] fileBytes = System.IO.File.ReadAllBytes(filepath);
return File(fileBytes, "application/pdf");
}
請記住,如果要訪問網絡共享,您的應用程序池需要在有權訪問所述網絡共享的身份下運行。
要在新選項卡中下載文件,請嘗試在客戶端 html 中使用<a href="/file.pdf" target="_blank">file</a>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.