我從 ftp 服務器獲取 pdf 檔案作為位元組,然后將其轉換為 ToBase64String,并在前面使用 iframe。有些檔案出現在前面,有些則沒有。
我從 ftp 服務器獲取檔案內容的代碼:
public byte[] GetFTPFile(string filePathFTP)
{
//String filePath = System.IO.Path.Combine(hosting.WebRootPath, "downloads") "/try.pdf" ;
//String ftpFilePath = "Images/taj123.png";
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(userName, password);
//NEED TO CHECK IF FILE IS EXIST
byte[] fileData = request.DownloadData(filePathFTP);
//System.IO.File.WriteAllBytes(filePath, fileData);
return fileData;
}
我在控制器中的代碼:
public ActionResult Details(int id)
{
File file = fileRepo.Find(id);
//file.Content = System.IO.Path.Combine(hosting.WebRootPath, "downloads") "/try.pdf";
file.Content = Convert.ToBase64String(ftpFiles.GetFTPFile(file.FilePath));
return View(file);
}
我在cshtml中的代碼:
<iframe src="data:application/pdf;base64,@Model.Content" type="application/pdf" class="w-100" style="height:100vh"></iframe>
uj5u.com熱心網友回復:
在你的控制器中試試這個:
return new FileContentResult(content, "application/pdf");
讓我知道結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/483056.html
標籤:C# 网 asp.net-mvc asp.net 核心 .net-core
