diff --git a/src/components/upload/ajax.js b/src/components/upload/ajax.js new file mode 100755 index 0000000..911affd --- /dev/null +++ b/src/components/upload/ajax.js @@ -0,0 +1,82 @@ +// https://github.com/ElemeFE/element/blob/dev/packages/upload/src/ajax.js + +function getError(action, option, xhr) { + const msg = `fail to post ${action} ${xhr.status}'`; + const err = new Error(msg); + err.status = xhr.status; + err.method = 'post'; + err.url = action; + return err; +} + +function getBody(xhr) { + const text = xhr.responseText || xhr.response; + if (!text) { + return text; + } + + try { + return JSON.parse(text); + } catch (e) { + return text; + } +} + +export default function upload(option) { + if (typeof XMLHttpRequest === 'undefined') { + return; + } + + const xhr = new XMLHttpRequest(); + const action = option.action; + + if (xhr.upload) { + xhr.upload.onprogress = function progress(e) { + if (e.total > 0) { + e.percent = e.loaded / e.total * 100; + } + option.onProgress(e); + }; + } + + const formData = new FormData(); + + if (option.data) { + Object.keys(option.data).map(key => { + formData.append(key, option.data[key]); + }); + } + + formData.append(option.filename, option.file); + + xhr.onerror = function error(e) { + option.onError(e); + }; + + xhr.onload = function onload() { + if (xhr.status < 200 || xhr.status >= 300) { + return option.onError(getError(action, option, xhr), getBody(xhr)); + } + + option.onSuccess(getBody(xhr)); + }; + + xhr.open('post', action, true); + + if (option.withCredentials && 'withCredentials' in xhr) { + xhr.withCredentials = true; + } + + 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); +} diff --git a/src/components/upload/index.js b/src/components/upload/index.js index e69de29..59bcf38 100644 --- a/src/components/upload/index.js +++ b/src/components/upload/index.js @@ -0,0 +1,3 @@ +import Upload from './upload.vue'; + +export default Upload; \ No newline at end of file diff --git a/src/components/upload/upload.vue b/src/components/upload/upload.vue index 5bc4676..9723f89 100644 --- a/src/components/upload/upload.vue +++ b/src/components/upload/upload.vue @@ -1,13 +1,312 @@ \ No newline at end of file diff --git a/test/routers/upload.vue b/test/routers/upload.vue index 5bc4676..e58eccf 100644 --- a/test/routers/upload.vue +++ b/test/routers/upload.vue @@ -1,5 +1,7 @@