fs.writeFileSync 写入空白文件
fs.writeFileSync writing blank file
我正在使用 fs 将 pdf 文件写入磁盘,pdf 文件包含 7 页内容,但是在写入文件时它没有任何内容。整个文件是空白的。下面是我为此使用的代码
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)
})
fs.writeFileSync
、fs.writeSync
和 fs.writeFile
之间存在混淆。
试试这个:
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)
}
如果结果不是您所期望的,您应该检查 response
变量的内容并确保它被正确填充。
参考文献:
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
我已尝试重新创建您的代码,但我不确定您使用哪个库来提取数据。这个例子对我来说很好用:
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)
}
});
编辑:
我的代码无法正常工作,因为我忘记将编码设置为 null。在请求的文档中,有这样的解释:
encoding - encoding to be used on setEncoding of response data. If
null, the body is returned as a 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). (Note: if
you expect binary data, you should set encoding: null.)
来自 https://www.npmjs.com/package/request#request-options-callback
writeFileSync
的编码可以安全删除。
我正在使用 fs 将 pdf 文件写入磁盘,pdf 文件包含 7 页内容,但是在写入文件时它没有任何内容。整个文件是空白的。下面是我为此使用的代码
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)
})
fs.writeFileSync
、fs.writeSync
和 fs.writeFile
之间存在混淆。
试试这个:
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)
}
如果结果不是您所期望的,您应该检查 response
变量的内容并确保它被正确填充。
参考文献:
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
我已尝试重新创建您的代码,但我不确定您使用哪个库来提取数据。这个例子对我来说很好用:
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)
}
});
编辑:
我的代码无法正常工作,因为我忘记将编码设置为 null。在请求的文档中,有这样的解释:
encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a 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). (Note: if you expect binary data, you should set encoding: null.)
来自 https://www.npmjs.com/package/request#request-options-callback
writeFileSync
的编码可以安全删除。