[英].NET FileNotFoundException on correct path?
我正在嘗試像這樣在我的應用程序中提供 PDF
string[] paths = {Environment.CurrentDirectory, "Content", "HelpDoc.pdf"};
string filePath = Path.Combine(paths);
try
{
return File(filePath, "application/pdf");
}
catch (FileNotFoundException e)
{...}
但是,在瀏覽器中點擊它時,我收到“FileNotFoundException:找不到文件:C:\Users...”。
我覺得我在這件事上失去了理智,有人有什么建議嗎?
對於其他有同樣問題的人,我將我的 pdf 移動到 wwwroot 目錄中一個名為“pdf”的新目錄。 然后我能夠像這樣訪問它:
string[] paths = {"wwwroot", "pdf", "HelpDoc.pdf"};
string filePath = Path.Combine(paths);
byte[] pdfBytes = System.IO.File.ReadAllBytes(filePath);
MemoryStream memoryStream = new MemoryStream(pdfBytes);
return new FileStreamResult(memoryStream, "application/pdf");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.