为什么 createReadStream 函数发送损坏的文件?
Why createReadStream function sending corrupt file?
我正在使用 fs 模块从 url 生成一个文件到我的本地系统。希望是成功的。但是当我尝试使用 createReadStream() 函数将该文件 post 分组时,它向我发送了一个损坏的文件。
目前我尝试使用 excel 文件。
function saveFileToLocal(urlData,fileName){
return new Promise ((res,rej) => {
https.get(urlData, function(response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(path+`/controller/localmaping/files/${fileName}`)
response.pipe(file);
file.on('finish', function() {
file.close(); // close() is async
});
res('done')
}
else{
rej('failed')
}
})
})
}
async function postAttachmentToGroup(groupid, senderId, text, urlData) {
let fileName = urlData.substring(urlData.lastIndexOf('/') + 1).split("?")[0] // File name
await saveFileToLocal(urlData,fileName)
console.log(fileName)
console.log(path)
request({
url: "https://graph.facebook.com/group_file_revisions",
headers: { "Content-Type": "form-data","Authorization":"Bearer "+config.ACCESS_TOKEN },
method: "POST",
formData:{"file":fs.createReadStream(path+`/controller/localmaping/files/${fileName}`)}
}, (err, res, body) => {
if (!err) {
let id = JSON.parse(body).id;
let request_body = {
"message": text + "\nPost by @[" + senderId + "]",
"formatting":"MARKDOWN",
"files": [ id ]
}
request({
"uri": `https://graph.workplace.com/${groupid}/feed`,
"qs": { "access_token": config.ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log(body)
console.log('Post Success in feed!')
}
else {
console.error("Unable to Post in feed" + err);
}
});
}
else {
console.error("Unable to Post in feed" + err);
}
})
}
我通过添加回调函数解决了这个问题。还添加了在文件发送过程成功后从本地存储中删除文件的功能。
function postAttachmentToGroup(groupid, senderId, text, urlData) {
let fileName = urlData.substring(urlData.lastIndexOf('/') + 1).split("?")[0]
https.get(urlData, function(response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(path+`/controller/localmaping/files/${fileName}`)
response.pipe(file);
file.on('finish', function() {
request({
url: "https://graph.facebook.com/group_file_revisions",
headers: { "Content-Type": "multipart/form-data","Authorization":"Bearer "+config.ACCESS_TOKEN },
method: "POST",
formData:{"file":fs.createReadStream(path+`/controller/localmaping/files/${fileName}`)}
}, (err, res, body) => {
if (!err) {
let id = JSON.parse(body).id;
console.log(id)
let request_body = {
"message": text + "\nPost by @[" + senderId + "]",
"formatting":"MARKDOWN",
"files": [ id ]
}
request({
"uri": `https://graph.workplace.com/${groupid}/feed`,
"qs": { "access_token": config.ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log(body)
console.log('Post Success in feed!')
fs.unlink(path+`/controller/localmaping/files/${fileName}`,function (err){
if(err){
console.log("error in deleting file")
}
else{
console.log("file deleted successfully")
}
})
}
else {
console.error("Unable to Post in feed" + err);
}
});
}
else {
console.error("Unable to Post in feed" + err);
}
})
});
}
else{
console.log('failed')
}
})
}
我正在使用 fs 模块从 url 生成一个文件到我的本地系统。希望是成功的。但是当我尝试使用 createReadStream() 函数将该文件 post 分组时,它向我发送了一个损坏的文件。
目前我尝试使用 excel 文件。
function saveFileToLocal(urlData,fileName){
return new Promise ((res,rej) => {
https.get(urlData, function(response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(path+`/controller/localmaping/files/${fileName}`)
response.pipe(file);
file.on('finish', function() {
file.close(); // close() is async
});
res('done')
}
else{
rej('failed')
}
})
})
}
async function postAttachmentToGroup(groupid, senderId, text, urlData) {
let fileName = urlData.substring(urlData.lastIndexOf('/') + 1).split("?")[0] // File name
await saveFileToLocal(urlData,fileName)
console.log(fileName)
console.log(path)
request({
url: "https://graph.facebook.com/group_file_revisions",
headers: { "Content-Type": "form-data","Authorization":"Bearer "+config.ACCESS_TOKEN },
method: "POST",
formData:{"file":fs.createReadStream(path+`/controller/localmaping/files/${fileName}`)}
}, (err, res, body) => {
if (!err) {
let id = JSON.parse(body).id;
let request_body = {
"message": text + "\nPost by @[" + senderId + "]",
"formatting":"MARKDOWN",
"files": [ id ]
}
request({
"uri": `https://graph.workplace.com/${groupid}/feed`,
"qs": { "access_token": config.ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log(body)
console.log('Post Success in feed!')
}
else {
console.error("Unable to Post in feed" + err);
}
});
}
else {
console.error("Unable to Post in feed" + err);
}
})
}
我通过添加回调函数解决了这个问题。还添加了在文件发送过程成功后从本地存储中删除文件的功能。
function postAttachmentToGroup(groupid, senderId, text, urlData) {
let fileName = urlData.substring(urlData.lastIndexOf('/') + 1).split("?")[0]
https.get(urlData, function(response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(path+`/controller/localmaping/files/${fileName}`)
response.pipe(file);
file.on('finish', function() {
request({
url: "https://graph.facebook.com/group_file_revisions",
headers: { "Content-Type": "multipart/form-data","Authorization":"Bearer "+config.ACCESS_TOKEN },
method: "POST",
formData:{"file":fs.createReadStream(path+`/controller/localmaping/files/${fileName}`)}
}, (err, res, body) => {
if (!err) {
let id = JSON.parse(body).id;
console.log(id)
let request_body = {
"message": text + "\nPost by @[" + senderId + "]",
"formatting":"MARKDOWN",
"files": [ id ]
}
request({
"uri": `https://graph.workplace.com/${groupid}/feed`,
"qs": { "access_token": config.ACCESS_TOKEN },
"method": "POST",
"json": request_body
}, (err, res, body) => {
if (!err) {
console.log(body)
console.log('Post Success in feed!')
fs.unlink(path+`/controller/localmaping/files/${fileName}`,function (err){
if(err){
console.log("error in deleting file")
}
else{
console.log("file deleted successfully")
}
})
}
else {
console.error("Unable to Post in feed" + err);
}
});
}
else {
console.error("Unable to Post in feed" + err);
}
})
});
}
else{
console.log('failed')
}
})
}