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

Time:05-26

I got pdf file from ftp server as byte then I converted it to ToBase64String and in front I used iframe. some files appear in the front and others are not.

My code to get file content from ftp server:

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;
        }

My code in controller:

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);
        }

My code in cshtml:

    <iframe src="data:application/pdf;base64,@Model.Content" type="application/pdf"  style="height:100vh"></iframe>

CodePudding user response:

Try this in your Controller:

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

Let me know about the result

  • Related