Node-Webkit下载PDF [英] Node-Webkit Download PDF

查看:103
本文介绍了Node-Webkit下载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个node-webkit应用程序,我从网站收到下载特定文件的请求。当我启动Web服务调用时,我会在response.body中找回该文件。我正在尝试使用fs api中的示例将pdf保存到我的本地文件夹,即:

I'm building a node-webkit app where I receive a request from a website to download a particular file. When I initiate the web service call I get back the file in the response.body. I'm trying to use the example in the fs api to save the pdf to my local folder i.e.:

(我将response.body传递给数据字段,并传递字符串'binary'以指定选项下的编码)

(i'm passing the response.body into the data field, and passing a string 'binary' to specify encoding under options)

var options = { encoding: 'binary' };
console.log('File name:' + fileName);
fileOperations.write(fileName, response.body, options, null);

在fileOperations中:

In fileOperations:

module.exports = {
    write: function (filename, data, options, callback) {
    fs.writeFile(filename, data, options, function (err) {
      if (err) throw err;
      console.log('It\'s saved!');
    });
  }
};

文件使用正确的文件名和扩展名以及文件大小保存到本地文件夹。但是,在预览中打开时,每个页面都是空白的我指定了错误的编码类型吗?

The file is saved to the local folder with the correct file name and extension, as well as file size. However when opened in preview each page is blank. Am I specifying the wrong encoding type?

推荐答案

这听起来与我遇到的问题类似。下载文件(不仅仅是pdf)导致了奇怪的结果。这更可能是你的问题....而不是fs功能。我们选择使用请求库(npm请求)并以这种方式执行下载,而不是使用内置节点http内容:

This sounds similar to a problem I had. Downloading files (not just pdf) resulted in strange results. This is more likely your issue....not the fs functions. Rather than using the built in node http stuff we chose to use the Request library (npm request) and performed downloads in this fashion:

 request({
      method: 'GET',
      uri: baseUrl + '/api/v1/documents/versions/contents/doc33',
      headers: {"Access-Control-Allow-Origin": baseUrl, "Cookie": cookie}
    }, function (error, response, body) {

      var contentDisp = response.headers['content-disposition'].split('"');
      var ext = contentDisp[1].split('.')[1];

      // you can rename the downloaded file (temp) and add the proper extension here...

    }).pipe(fs.createWriteStream('temp')); // you can append a directory to the temporary name as well..
}

我会试一试,看看它是否适合你。跨平台的文件可能很困难。

I would give this a shot and see if it works for you. Working with files across platforms can be difficult.

这篇关于Node-Webkit下载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆