打开与科尔多瓦的PDF字节流文件字节、科尔、多瓦、文件

2023-09-10 18:05:54 作者:′战斗法师┒

我有与创建一个iTextSharp的PDF文档的方法的.NET Web服务。

I've got a .net web service with a method which creates an iTextSharp PDF document.

我有一个科尔多瓦/ JqueryMobile基于应用程序,它在它的景色之一调用此方法。

I've got a Cordova / JqueryMobile based app which calls this method on one of its views.

Web服务发送的文件作为字节流。

The web service sends the document as a byte stream.

我不能让科尔多瓦应用程序,以显示该文件。

I cannot get the Cordova app to display the file.

Web服务

<OperationContract()> _
<WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json)> _
Public Function GetPDF(ByVal accountID As Int64) As Byte()
    Dim myPDF As Document = New Document

    Dim msPDFData As MemoryStream = New MemoryStream()
    Dim writer As PdfWriter = PdfWriter.GetInstance(myPDF, msPDFData)
    myPDF.Open()
    myPDF.Add(New Paragraph("I'm a pdf!"))

    Dim pdfData As Byte() = msPDFData.ToArray()

    Return pdfData
End Function

很简单。

Ajax调用

var dataString = JSON.stringify({
    accountID: '309'
});

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: '/GetPDF',
    data: dataString,
    processData: true,
    dataType: "json",
    success: function (response) {
    var reader = new FileReader();        
    reader.readAsArrayBuffer(response.d);
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(new Uint8Array(evt.target.result));
    };
    },
    error: function (a, b, c) {
        alert('file error')
    }
});

这是没有做任何事情。但response.d确实含有似乎是文件的字节。

This isn't doing anything. But response.d does contain what seems to be the bytes of the document.

我想科尔多瓦解析字节数组,并打开一个PDF阅读器显示文档,可以选择保存该文件。

I am want Cordova to parse the byte array and open a PDF reader to display the document, with the option to save the file.

我想我可以使用FileTransfer.download科尔多瓦方法,但这个例子有一个固定的URI,而我需要调用Web服务,我还没有能够互换的固定URI与我Ajax调用 - 所以我不知道这是否就是答案。

I thought I could use the FileTransfer.download Cordova method, but the example has a fixed uri, whereas I would need to call a web service and I haven't yet been able to swap that fixed uri with my ajax call - so I don't know if that is the answer.

推荐答案

我找到了答案,用SAURABH的帮助下,在this SO质疑

I found the answer, with the help of Saurabh at this SO question

在我最初的实现,我希望以某种方式直接从数据流中打开该文件。但事实证明,你必须创建一个物理文件,并写入数据到,然后打开它。

In my original implementation, I was hoping to somehow open the file directly from the stream. But it turns out you have to create a physical file and write the data to that and then open it.

所以,我更新code将来自web服务成二进制数组中的字节数组,然后编写到文件:

So, my updated code converts the byte array from the webservice into a binary array, and then write that to the file:

var dte = getCurDateOnly(), fileData, UTF8_STR, BINARY_ARR, dataString = JSON.stringify({
    accountID: '309'
});

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: '/GetPDF',
    data: dataString,
    processData: true,
    dataType: "json",
    success: function (response) {
        fileData = response.d;
        UTF8_STR = new Uint8Array(response.d);  // Convert to UTF-8...                
        BINARY_ARR = UTF8_STR.buffer; // Convert to buffer...
        getFS();
    },
    error: function (a, b, c) {
        alert('file error')
    }
});

function getFS() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getDirectory("MyDIR", { create: true }, gotDir);
}

function gotDir(dirEntry) {
    dirEntry.getFile("MyFILE" + dte + ".pdf", { create: true, exclusive: false }, gotFile);
}

function gotFile(fileEntry) {
    fileEntry.createWriter(function (writer) {
        writer.onwrite = function (evt) {
            console.log("write success");
            listDir();
        };
        writer.write(BINARY_ARR);
    }, function (error) {
        console.log(error);
    });
}
function fail() {
    console.log('pdf fail function called');
}

由于 SAURABH 的最后一块拼图。

 
精彩推荐
图片推荐