Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从节点上载时损坏的文件_Javascript_Node.js_Image_File Upload_Corruption - Fatal编程技术网

Javascript 从节点上载时损坏的文件

Javascript 从节点上载时损坏的文件,javascript,node.js,image,file-upload,corruption,Javascript,Node.js,Image,File Upload,Corruption,我正在将powerpoint文件转换为单个图像,当我从节点上载时,生成的图像最终会损坏 我使用的npm模块是和 我从我的节点应用程序上传一个文件,如下所示: var request = require('request'); var FormData = require('form-data'); function processPresentation(fileName,fileLoc,userId,customerId){ var data = new FormData();

我正在将powerpoint文件转换为单个图像,当我从节点上载时,生成的图像最终会损坏

我使用的npm模块是和

我从我的节点应用程序上传一个文件,如下所示:

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);
        }
    });
}
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
它经过我的转换过程,我得到一个像这样的文件作为输出:

如果打开powerpoint文件并查看文本,下面是所有这些内容的实际含义:

当我使用Postman并上传相同的文件时,如下所示:

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);
        }
    });
}
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
我得到如下输出:

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);
        }
    });
}
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


这正是我想要的。

尝试删除对表单数据的要求,然后执行以下操作:

var options = {
    url: config.getConverter()+"?tenant="+customerId+"&author="+userId+"&name="+fileName+"&ext=ppt",
    method: 'POST',
    formData: {
        my_file: fs.createReadStream(fileLoc)
    },
    headers:{'x-auth-token':token,'Content-Type':'application/vnd.openxmlformats-officedocument.presentationml.presentation'}
};
重要信息:有
formData
vs
form

这样做可以修复它:

var length = fs.statSync(fileLoc).size;
    console.log('length is',length);
    var req = request.post({uri: config.getConverter()+"?tenant="+customerId+"&author="+userId+"&name="+fileName+"&ext=ppt",
        headers:{
            'x-auth-token':token,
            'Content-Type':'application/vnd.openxmlformats-officedocument.presentationml.presentation',
            'content-length':length
        }
    });
    fs.createReadStream(fileLoc).pipe(req).pipe(process.stdout);

好的,我认为问题在于您正在发送mime/多部分编码的数据,而您只想将文件作为文章的原始正文发送。有几种方法可以做到这一点

选项#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));
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);
这种技术实际上是将文件流式传输为您请求的原始帖子正文

选项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'}
};

fs.createReadStream(fileLoc).pipe(request.post(options));
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
选项发布它。这会将文章正文设置为原始,而不是使用mime/多部分编码,因为您使用
formData

时,它会从我的服务器发出
无效请求
,因为它需要一个输入流,而当我执行上述代码时,它不会得到该流。实际上,我的文件被意外删除。我还原了它,现在运行这段代码会给我一个损坏的文件,
----------------------------186177887193287547417109内容处置:表单数据;name=“6cbd90c5-9e73-439f-8eea-2af77d8e8fdd”
如果您使用文本编辑器打开文件,请在其开头处!您根本不需要表单或多部分编码的数据!我将发布一个单独的答案…谢谢!!选项2有效,但选项1无效。我仍然使用选项#1获取损坏的文件,不知道为什么。。。有什么想法吗?