简体   繁体   English

fs.writeFileSync写入空白文件

[英]fs.writeFileSync writing blank file

I am using fs for writing a pdf file to disk, the pdf file contains 7 pages which have contents, however when the file is getting written it doesn't have any content in it. 我正在使用fs将pdf文件写入磁盘,该pdf文件包含包含内容的7页,但是当文件被写入时,其中没有任何内容。 The whole file is blank. 整个文件为空。 Below is the code I am using for this 以下是我为此使用的代码

request.get({
        url: spConfig.host + spConfig.sitecollection + '_api/web/GetFileByServerRelativeUrl(\'/sites/MSDS/Construction/ProductLabels/SD32382_-_ADVA Cast 596_(B2).pdf\')/$value',
        headers: headers,
        json: true
    }).then(response => {
      console.log('Inside Success')
      // console.log(response)
      // let writeStream = fs.createWriteStream(__dirname+'/wsdl/attachment.pdf')
      console.log(response.length)
       try {
            fs.writeFileSync(__dirname+'/wsdl/attachment.pdf', response,'binary')
            console.log('File has been written successfully')
        } catch (err) {
            console.log('Error in writing file')
            console.log(err)
        }
    }).catch(err => {
        spResponseCount = spResponseCount + 1  
        reject(err)
    })

There's a mixup between the fs.writeFileSync , fs.writeSync and the fs.writeFile . fs.writeFileSyncfs.writeSyncfs.writeFile之间存在混淆。

Try with this: 试试这个:

try {
    fs.writeFileSync(__dirname+'/wsdl/attachment.pdf', response);
    console.log('Success in writing file')
} catch (err) {
    console.log('Error in writing file')
    console.log(err)
}

If the result is not what you expect, you should give a check to the content of the response variable and make sure that it's properly filled. 如果结果与预期不符,则应检查response变量的内容,并确保已正确填充。

References: 参考文献:

https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_writefile_file_data_options_callback https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_writefilesync_file_data_options https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_writefile_file_data_options_callback https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_writessync_file_data_options


I've tried to recreate your code but I'm not sure what library you're using to extract data. 我试图重新创建您的代码,但不确定使用哪个库提取数据。 This example works fine with me: 这个例子适合我:

let request = require('request-promise');
let fs = require('fs');

request.get({
    url: 'http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf',
    encoding: null
})
.then(response => {

    try {
      fs.writeFileSync(__dirname+'/attachment.pdf', response);
      console.log('Success in writing file')
    } catch (err) {
      console.log('Error in writing file')
      console.log(err)
    }
});

EDIT: 编辑:

My code was not working properly because I forgot to set the encoding to null. 我的代码无法正常工作,因为我忘记了将编码设置为null。 In the documentation of the request, there's this explanation: 在请求的文档中,有以下解释:

encoding - encoding to be used on setEncoding of response data. encoding-用于响应数据的setEncoding的编码。 If null, the body is returned as a Buffer. 如果为null,则将正文作为Buffer返回。 Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). 其他任何内容(包括未定义的默认值)都将作为编码参数传递给toString()(这意味着默认情况下它实际上是utf8)。 (Note: if you expect binary data, you should set encoding: null.) (注意:如果需要二进制数据,则应将编码设置为:null。)

From https://www.npmjs.com/package/request#request-options-callback 来自https://www.npmjs.com/package/request#request-options-callback

The encoding of the writeFileSync can be safely dropped. 可以安全地删除writeFileSync的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM