开发者

stream a pdf to a .net webbrowser object on a winform

开发者 https://www.devze.com 2022-12-09 09:57 出处:网络
I have a webbrowser object on a winform that I would like to use to display a pdf. The pdf resides on a ftp server.I have been able to show the pdf by downloading it

I have a webbrowser object on a winform that I would like to use to display a pdf. The pdf resides on a ftp server. I have been able to show the pdf by downloading it to the disk and pointing the webbrowser object to it (navigate), but I want to stream it for security reasons. Has anyone been able to stream a pdf to a webbrowser that is located on a .Net winform?

    Dim URI As String = host & targetFilename
    Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)

    ftp.Credentials = New System.Net.NetworkCredential(userName, passWord)
    ftp.KeepAlive = False
    ftp.UseBinary = True
    ftp.Method = System.Net.WebRequestMethod开发者_运维百科s.Ftp.DownloadFile

    Using webResp As System.Net.FtpWebResponse = DirectCast(ftp.GetResponse(), System.Net.FtpWebResponse)
        Using respStream As Stream = webResp.GetResponseStream
            If GetFileExtension(targetFilename) = "PDF" Then
                WebBrowser1.DocumentStream = respStream
                Application.DoEvents()
            End If

            respStream.Close()
        End Using
    End Using


Since you're already using the WebBrowser control; why not have it point to a local html file that includes an embed tag:

<embed src="ftp://ftpserver/yourpdf.pdf" />

I haven't tested it, but the pdf should be served within the context of the control.


I would recommend using a PDF Viewer Control rather than a web browser control, as a web browser control will require that the client have a PDF Viewer installed.

This will also allow you to stream a document to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消