从 ftp 服务器获取 pdf 文件并查看它,而无需在 mvc 核心中下载

get pdf file from ftp server and view it without download it in mvc croe

我从 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>

在您的控制器中试试这个:

return new FileContentResult(content, "application/pdf");

让我知道结果