Request upload file error

  node.js, question

I usemultipartyParse the file uploaded by the front end, and then use therequestUpload to file server.

form.on('part', function(part) {
 if (part.filename) {
 var formData = {
 name: part.filename,
 my_file: part
 url:  '' ,
 formData: formData
 }, function optionalCallback(err, httpResponse, body) {
 if (err) {
 return console.error('upload failed:', err);
 console.log('Upload successful!   Server responded with:', body);

The official example of request is to pass the readStream of the file into formdata. For example

var formData = {
 My _ file: fs.createreadstream (_ _ dirname plus' /unicycle.jpg'),

But i don’t want to create temporary files before getting readStream.Since multiparty’s part is also a readable stream, transform streamWhich has both readStream and writeStream interfaces, so I want to pass part directly into formdata. However, the following error was encountered

TypeError: form-data: Invalid non-string/buffer chunk

Since the file server is a remote online server, I do not know the details of it. However, it does support the upload of formdata of readStream created from files. butpartNot at all.

What is the difference between transform stream and fs.createStream? Is there any way to transfer the data from transform stream to readStream? Thank you

Well, I found the answer myself.

Use form-data

Part mentioned in the question is a transform stream, which is generally used to process datatransform, that is, transformation. It can be read or written, and in fact it will only be output when there is data input. Think of it as a box for transforming data.

The requset module can post form data, like this.

var form = {
 name: 'xxx',
 my_file: streamObj
 url:  '' ,
 formData: form

This form is just a simple js object, and request will convert it into formdata.
Request can get all the information it needs from streamObj, but this streamonly supports fs.readStream, http.response For other stream, file-related information is required.

Below is the original text, which probably means what is said above.

Form-Data can recognize and fetch all the required information from common types of streams (fs.readStream, http.response and mikeal’s request), for some other types of streams you’d need to provide “file”-related information manually:

So, you must use the following method to construct the form, the original form-data, of course, first you have to npm install form-data.

var form = new FormData();
 form.append('fieldname', part, {
 filename: part.filename,
 contentType: part.headers['content-type'],
 knownLength: part.byteCount

Filename,headers,byteCount are all part’s own attributes.

Set http headertransfer-encoding: chunked

When the part event is triggered, the file part in the client form is describedStart uploadingAt the current server, at this time, we will send part to send data tofile serverHttp request for. At this point, you must specify a header for this request

Transfer-Encoding: chunked

Why is this? Because, the content-length is uncertain at this time, because the client’s data has not yet been uploaded. We must inform the file server,The length of the request is still uncertain, you must wait until I say stop..

This is the meaning of setting this header.