Viewing PDF file after obtaining bytes

Asked

Viewed 249 times

1

I would like to view a file .PDF staying at a FTP. I can get the Stream through the bytes, but I can’t create the file in a browser.

Follow the code so far:

 private void btnVerFilePdf_Click(object sender, EventArgs e)
    {
        WebClient request = new WebClient();
        string url = "ftp://nfsaai.com.br@hostip/" + "fileexample.pdf";
        request.Credentials = new NetworkCredential("userstr", "passwordstr");


        try
        {        
            byte[] newFileData = request.DownloadData(url);
            string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
            Console.WriteLine(fileString); 
        }
        catch (Exception error)
        {
            MessageBox.Show(String.Format("erro : {0}", error.Message));       
        }       
    }
  • Windowsform or Webforms?

  • Windowsform....

  • 2

    In that case you will need a component to display in your application or save it locally and open the file

  • Got it @Leandroangelo I thought I could with the bytes caught build the document in a browser.

  • Maybe with Base64 I’ll even get

1 answer

0

The simplest form in WinForms is using the component Web Browser.

Example:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //No seu caso pode por direto o caminho do FTP:
        RenderizarPDF(@"C:\CaminhoPDF\MeuDocumento.pdf");
    }

    private void RenderizarPDF(string filePath)
    {
        if (!string.IsNullOrWhiteSpace(filePath))
        {
            webBrowser1.Navigate(@filePath);
        }
    }
}

If you have a problem with displaying the PDF directly from FTP in the WebBrowser I suggest to implement a feature that saves the PDF Stream that you have already obtained in a temporary folder with a Guid which identifies the file and after the completion of the reading you can delete the PDF that has that name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.