簡體   English   中英

.NET FileNotFoundException 在正確的路徑上?

[英].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 的屬性“CopyToOutputDirectory”是“始終復制”
  • 我已經多次驗證該路徑與我的文件資源管理器和 Rider 的復制路徑選項中的位置匹配

我覺得我在這件事上失去了理智,有人有什么建議嗎?

對於其他有同樣問題的人,我將我的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM