From a87b6e2e94e026880cdacb98934307007c23772d Mon Sep 17 00:00:00 2001 From: baiyaaaaa Date: Thu, 9 Mar 2017 14:30:00 +0800 Subject: [PATCH] add custom http requrest --- examples/docs/en-US/upload.md | 1 + examples/docs/zh-CN/upload.md | 1 + packages/upload/src/ajax.js | 5 +---- packages/upload/src/index.vue | 6 ++++-- packages/upload/src/upload.vue | 6 ++++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/docs/en-US/upload.md b/examples/docs/en-US/upload.md index 3a4befe6f..bc24b6391 100644 --- a/examples/docs/en-US/upload.md +++ b/examples/docs/en-US/upload.md @@ -364,6 +364,7 @@ thumbnail-mode | whether thumbnail is displayed | boolean | — | false file-list | default uploaded files, i.e: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] | array | — | [] list-type | type of fileList | string | text/picture/picture-card | text | auto-upload | whether to auto upload file | boolean | — | true | +http-request | override default xhr behavior, allowing you to implement your own upload-file's request | function | — | — | ### Events | Event Name | Description | Parameters | diff --git a/examples/docs/zh-CN/upload.md b/examples/docs/zh-CN/upload.md index 05ca46974..7eaae13fc 100644 --- a/examples/docs/zh-CN/upload.md +++ b/examples/docs/zh-CN/upload.md @@ -413,6 +413,7 @@ | list-type | 文件列表的类型 | string | text/picture/picture-card | text | | auto-upload | 是否在选取文件后立即进行上传 | boolean | — | true | | file-list | 上传的文件列表, 例如: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}] | array | — | [] | +| http-request | 覆盖默认的上传行为,可以自定义上传的实现 | function | — | — | ### Methods | 方法名 | 说明 | 参数 | diff --git a/packages/upload/src/ajax.js b/packages/upload/src/ajax.js index 56cd73c7e..f5bfa14a0 100644 --- a/packages/upload/src/ajax.js +++ b/packages/upload/src/ajax.js @@ -75,14 +75,11 @@ export default function upload(option) { const headers = option.headers || {}; - // if (headers['X-Requested-With'] !== null) { - // xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - // } - for (let item in headers) { if (headers.hasOwnProperty(item) && headers[item] !== null) { xhr.setRequestHeader(item, headers[item]); } } xhr.send(formData); + return xhr; } diff --git a/packages/upload/src/index.vue b/packages/upload/src/index.vue index 992fa6ae3..de48886d0 100644 --- a/packages/upload/src/index.vue +++ b/packages/upload/src/index.vue @@ -85,7 +85,8 @@ export default { listType: { type: String, default: 'text' // text,picture,picture-card - } + }, + httpRequest: Function }, data() { @@ -228,7 +229,8 @@ export default { 'on-success': this.handleSuccess, 'on-error': this.handleError, 'on-preview': this.onPreview, - 'on-remove': this.handleRemove + 'on-remove': this.handleRemove, + httpRequest: this.httpRequest }, ref: 'upload-inner' }; diff --git a/packages/upload/src/upload.vue b/packages/upload/src/upload.vue index b99dfbc82..3ac630302 100644 --- a/packages/upload/src/upload.vue +++ b/packages/upload/src/upload.vue @@ -37,7 +37,8 @@ export default { }, fileList: Array, autoUpload: Boolean, - listType: String + listType: String, + httpRequest: Function }, data() { @@ -93,7 +94,8 @@ export default { } }, post(rawFile) { - ajax({ + const request = this.httpRequest || ajax; + request({ headers: this.headers, withCredentials: this.withCredentials, file: rawFile,