From 285d6d43b2da34126d3892756011b92651a4ac2e Mon Sep 17 00:00:00 2001 From: Zation Date: Sat, 17 Aug 2019 18:15:07 +0800 Subject: [PATCH] add request method option --- README.md | 1 + src/AjaxUploader.jsx | 1 + src/request.js | 6 +++--- tests/request.spec.js | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f03b6acd..73cc5ec9 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ React.render(, container); |supportServerRender | boolean | false| whether to support server render | |onReady | function | | only call when supportServerRender is true, upload is rendered completely | |action| string | function(file): string | Promise<string> | | form action url | +|method | string | post | request method | |directory| boolean | false | support upload whole directory | |data| object/function(file) | | other data object to post or a function which returns a data object | |headers| object | {} | http headers to post, available in modern browsers | diff --git a/src/AjaxUploader.jsx b/src/AjaxUploader.jsx index a2c556ac..8d40cce2 100644 --- a/src/AjaxUploader.jsx +++ b/src/AjaxUploader.jsx @@ -160,6 +160,7 @@ class AjaxUploader extends Component { file: transformedFile, headers: props.headers, withCredentials: props.withCredentials, + method: props.method || 'post', onProgress: onProgress ? e => { onProgress(e, file); } : null, diff --git a/src/request.js b/src/request.js index 361da4e5..f79ed16c 100644 --- a/src/request.js +++ b/src/request.js @@ -1,8 +1,8 @@ function getError(option, xhr) { - const msg = `cannot post ${option.action} ${xhr.status}'`; + const msg = `cannot ${option.method} ${option.action} ${xhr.status}'`; const err = new Error(msg); err.status = xhr.status; - err.method = 'post'; + err.method = option.method; err.url = option.action; return err; } @@ -68,7 +68,7 @@ export default function upload(option) { }; - xhr.open('post', option.action, true); + xhr.open(option.method, option.action, true); // Has to be after `.open()`. See https://github.com/enyo/dropzone/issues/179 if (option.withCredentials && 'withCredentials' in xhr) { diff --git a/tests/request.spec.js b/tests/request.spec.js index d7a46342..113068fc 100644 --- a/tests/request.spec.js +++ b/tests/request.spec.js @@ -16,6 +16,7 @@ const option = { filename: 'a.png', file: 'foo', headers: { from: 'hello' }, + method: 'post', }; describe('request', () => {