I am trying to upload on server file, but it gives me the response that file isn't selected. Doing the same thing as in documentation of axios and form-data, but still have errors.
let form = new FormData();
form.append('file', fs.createReadStream('./files/50.jpg'));
const config = {
headers: {
...form.getHeaders(),
Authorization: token
}
}
axios.post(url, form, config)
.then(response => console.log(response.data, response.status))
.catch(err => console.error(err.config, err.response.data))
And the response is
{
url: 'https://example.com',
method: 'post',
data: FormData {
_overheadLength: 148,
_valueLength: 0,
_valuesToMeasure: [ [ReadStream] ],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: true,
_streams: [],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------897634953482711246524185',
_events: [Object: null prototype] { error: [Function: handleStreamError] },
_eventsCount: 1
},
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'multipart/form-data; boundary=--------------------------897634953482711246524185',
Authorization: 'Bearer token',
'User-Agent': 'axios/0.19.2'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus]
} { errors: [ { code: 65536, message: 'Must upload one file' } ] }
Help me please how to upload file, where is the error? Or maybe u can give some alternative ways to solve this case.
FormData?form.submit-method? it's documented on the npm-page, does that work?