diff --git a/.gitignore b/.gitignore index 6ac12083..7b570816 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ lib coverage yarn.lock es +package-lock.json diff --git a/examples/customRequest.html b/examples/customRequest.html new file mode 100644 index 00000000..48cdce85 --- /dev/null +++ b/examples/customRequest.html @@ -0,0 +1 @@ +placeholder diff --git a/examples/customRequest.js b/examples/customRequest.js new file mode 100644 index 00000000..9e9f8d2f --- /dev/null +++ b/examples/customRequest.js @@ -0,0 +1,83 @@ +/* eslint no-console:0 */ +import React from 'react'; +import ReactDOM from 'react-dom'; +import Upload from 'rc-upload'; +import axios from 'axios'; + +const uploadProps = { + action: '/upload.do', + multiple: false, + data: { a: 1, b: 2 }, + headers: { + Authorization: '$prefix $token', + }, + onStart(file) { + console.log('onStart', file, file.name); + }, + onSuccess(ret, file) { + console.log('onSuccess', ret, file.name); + }, + onError(err) { + console.log('onError', err); + }, + onProgress({ percent }, file) { + console.log('onProgress', `${percent}%`, file.name); + }, + customRequest({ + action, + data, + file, + filename, + headers, + onError, + onProgress, + onSuccess, + withCredentials, + }) { + // EXAMPLE: post form-data with 'axios' + const formData = new FormData(); + if (data) { + Object.keys(data).map(key => { + formData.append(key, data[key]); + }); + } + formData.append(filename, file); + + axios + .post(action, formData, { + withCredentials, + headers, + onUploadProgress: ({ total, loaded }) => { + onProgress({ percent: Math.round(loaded / total * 100).toFixed(2) }, file); + }, + }) + .then(({ data: response }) => { + onSuccess(response, file); + }) + .catch(onError); + + return { + abort() { + console.log('upload progress is aborted.'); + }, + }; + }, +}; + +const Test = () => { + return ( +