Rectangle 27 0

javascript Corrupt files when uploading from Node?


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

Read the File then Post

Here you are reading the file into a string and posting it using the body option. This sets the post body raw rather than using a mime/multipart encoding as you get with formData.

Ok, I think that the problem is that you are sending mime/multipart encoded data and you just want to send the file as the raw body of the post. There are a couple ways to do this.

This technique is actually streaming the file as the raw post body for your request.

Note
Rectangle 27 0

javascript Corrupt files when uploading from Node?


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);
Note
Rectangle 27 0

javascript Corrupt files when uploading from Node?


----------------------------186177887193287547417109 Content-Disposition: form-data; name="6cbd90c5-9e73-439f-8eea-2af77d8e8fdd"
form
formData
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'}
};

That gives me an invalid request from my server, because it expects an input stream, which it doesn't get when I do the code above.

Note