从VB pdf字节数组到(Javascript,HTML5)pdf

时间:2015-04-17 17:37:52

标签: javascript vb.net html5 pdf bytearray

我尝试使用来自字节数组的Javascript在webbrowser中显示(或下载).pdf。我从这篇文章中构建了我的代码:

 http://stackoverflow.com/questions/20401006/show-a-pdf-stream-in-a-new-window 

我收到此错误:

ERROR Request URL Too Long HTTP Error 414. The request URL is too long.

我的代码使用$ ajax调用VB WebMethod并接收base64String(.pdf)。 .pdf由SSRS报告服务器生成。

VB

Public Function testPDF(data As String) As String

    Try
        Dim strReportUser As String = Bidon.utilisateur
        Dim strReportUserPW As String = Bidon.motDePasse
        Dim strReportUserDomain As String = Bidon.domaine
        Dim sTargetURL As String = "an URL" + data
        Dim req As HttpWebRequest = WebRequest.Create(sTargetURL)
        req.PreAuthenticate = True
        req.Credentials = New System.Net.NetworkCredential(strReportUser, strReportUserPW, strReportUserDomain)
        Dim HttpWResp As HttpWebResponse = req.GetResponse()
        Dim fStream As Stream = HttpWResp.GetResponseStream()
        Dim fileBytes As Byte() = ReadFully(fStream)
        Return System.Convert.ToBase64String(fileBytes)
    Catch ex As Exception
        Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name + "[exception : " + ex.ToString() + "]")
    End Try

    Return Nothing
End Function

Public Function ReadFully(input As Stream) As Byte()

    Using MemoryStream As New MemoryStream()

        input.CopyTo(MemoryStream)
        Return MemoryStream.ToArray()

    End Using
End Function

JS

success: function (reponse) {
    var pdfwin = window.open(escape(reponse.d),"Titre","");
}

1 个答案:

答案 0 :(得分:0)

如果不使用.aspx,我找不到任何解决方案......就在这里。我希望有一天能帮助别人。

.ASPX

<!-- inside body -->
        <%testPDF(a number)%>

VB

Partial Class AfficherPdf
Inherits System.Web.UI.Page

Public Function testPDF(data As String) As Stream

    Dim autorise As Boolean = True ' remplacer par un appel de méthode validant si l'utilisateur est autorisé à imprimer ce pdf

    If (autorise) Then 'un utilisateur ne doit pas pouvoir imprimer des factures auquelles il n'a pas accès
        Try
            Dim strReportUser As String = Bidon.utilisateur
            Dim strReportUserPW As String = Bidon.motDePasse
            Dim strReportUserDomain As String = Bidon.domaine
            Dim sTargetURL As String = "... An URL ..." + data
            Dim req As HttpWebRequest = WebRequest.Create(sTargetURL)
            req.PreAuthenticate = True
            req.Credentials = New System.Net.NetworkCredential(strReportUser, strReportUserPW, strReportUserDomain)
            Dim HttpWResp As HttpWebResponse = req.GetResponse()
            Dim fStream As Stream = HttpWResp.GetResponseStream()
            Dim fileBytes As Byte() = ReadFully(fStream)
            Response.Clear()
            Response.ContentType = "application/pdf"
            Response.AddHeader(
            "content-length", fileBytes.Length.ToString())
            Response.BinaryWrite(fileBytes)
            Response.Flush()
            Response.End()
            Return fStream
        Catch ex As Exception
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name + "[exception : " + ex.ToString() + "]")
        End Try
    End If

    Return Nothing
End Function

Public Function ReadFully(input As Stream) As Byte()

    Using MemoryStream As New MemoryStream()

        input.CopyTo(MemoryStream)
        Return MemoryStream.ToArray()

    End Using
End Function

结束班

JS

window.open("AfficherPdf.aspx","_blank");