我正在将 powerpoint 文件转换为单个图像,当我从 Node 上传时,生成的图像最终损坏。
我使用的 npm 模块是 request和 node-form-data .
我从 Node 应用程序上传一个文件,如下所示:
var request = require('request');
var FormData = require('form-data');
function processPresentation(fileName,fileLoc,userId,customerId){
var data = new FormData();
data.append('my_file',fs.createReadStream(fileLoc));
var options = {
url: config.getConverter()+"?tenant="+customerId+"&author="+userId+"&name="+fileName+"&ext=ppt",
method: 'POST',
form:data,
headers:{'x-auth-token':token,'Content-Type':'application/vnd.openxmlformats-officedocument.presentationml.presentation'}
};
request.post(options,function(error,response,body){
console.log(body);
if(error){
console.log('error',error);
}
if(!response){
}
if(response.statusCode === 200){
addPresentation(JSON.parse(body),userId);
}
});
}
它经历了我的转换过程,我得到一个像这样的文件作为输出:
如果您打开 powerpoint 文件并查看文本,这就是所有这些内容的实际含义: http://pastebin.com/Dbh0JPKA
当我使用 Postman 并上传相同的文件时,如下所示:
POST HTTP/1.1
Host: xxxx.xxxxxxxxxx.net?name=00a94ec9-8f70-4279-8972-f49935cda295&ext=ppt&tenant=543840f80019abda4937a9e2&author=543bef549f8d54a53a02f6d9
Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
x-auth-token: 772a5c0c023a447f68a9ac4fb2bb4bd39bafeb16b753df2222ffc835750cbbe6a4ef9ee82fab0902f39bc26851016a873d44c91a64f67de5e10044ef0787cebe
Cache-Control: no-cache
Postman-Token: 74aff430-f6b8-c7cd-d477-b9cc35897bb7
undefined
我得到这样的输出:
这就是我想要的。
最佳答案
好吧,我认为问题是您正在发送 mime/multipart 编码数据,而您只想将文件作为帖子的原始正文发送。有几种方法可以做到这一点。
选项#1,流式传输文件:
var options = {
url: config.getConverter()+"?tenant="+customerId+"&author="+userId+"&name="+fileName+"&ext=ppt",
headers:{'x-auth-token':token,'Content-Type':'application/vnd.openxmlformats-officedocument.presentationml.presentation'}
};
fs.createReadStream(fileLoc).pipe(request.post(options));
此技术实际上是将文件作为您的请求的原始帖子正文进行流式传输。
选项#2,阅读文件然后发布:
var options = {
url: config.getConverter()+"?tenant="+customerId+"&author="+userId+"&name="+fileName+"&ext=ppt",
headers:{'x-auth-token':token,'Content-Type':'application/vnd.openxmlformats-officedocument.presentationml.presentation'},
body: fs.readFileSync(fileLoc)
};
request.post(options, callback);
在这里,您将文件读入字符串并使用 body
选项发布它。这会将帖子正文设置为原始,而不是像使用 formData
那样使用 mime/multipart 编码。
关于javascript - 从 Node 上传时文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26350068/