【问题标题】:Opening a pdf byte stream file with Cordova使用 Cordova 打开 pdf 字节流文件
【发布时间】:2014-07-25 21:36:40
【问题描述】:

我有一个 .net Web 服务,其中包含创建 iTextSharp PDF 文档的方法。

我有一个基于 Cordova / JqueryMobile 的应用程序,它在其中一个视图上调用此方法。

Web 服务将文档作为字节流发送。

我无法让 Cordova 应用程序显示文件。

网络服务

<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 确实包含似乎是文档的字节。

我希望 Cordova 解析字节数组并打开 PDF 阅读器来显示文档,并带有保存文件的选项。

我以为我可以使用 FileTransfer.download Cordova 方法,但该示例有一个固定的 uri,而我需要调用一个 Web 服务,我还不能用我的 ajax 调用交换那个固定的 uri -所以我不知道这是否是答案。

【问题讨论】:

  • 您是否确认您正在生成有效的 PDF?最后我记得你需要在使用它之前在你的文档上调用Close()。您是否在 Cordova 方面遇到了异常或任何情况?
  • 谢谢,我没有在文档上调用 close()。

标签: jquery asp.net ajax itextsharp cordova-3


【解决方案1】:

我在 Saurabh 的帮助下找到了答案,this SO question

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

因此,我更新的代码将字节数组从 web 服务转换为二进制数组,然后将其写入文件:

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 完成最后的拼图。

【讨论】:

  • 我已经尝试过使用 JPEG 图像,但它似乎不太奏效。如果我将 image.src 设置为使用 writer.write 函数写入的文件,它只会显示一个灰色的小框,而不是实际的图像。对此有何建议?
猜你喜欢
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 2015-01-09
相关资源
最近更新 更多