|
|
@@ -1,85 +1,85 @@
|
|
|
-function getErrorup(action, option, xhr) {
|
|
|
+function getErrorup (action, option, xhr) {
|
|
|
let msg = ''
|
|
|
if (xhr.response) {
|
|
|
- msg = `${xhr.status} ${xhr.response.error || xhr.response}`;
|
|
|
+ msg = `${xhr.status} ${xhr.response.error || xhr.response}`
|
|
|
} else if (xhr.responseText) {
|
|
|
- msg = `${xhr.status} ${xhr.responseText}`;
|
|
|
+ msg = `${xhr.status} ${xhr.responseText}`
|
|
|
} else {
|
|
|
- msg = `fail to post ${action} ${xhr.status}`;
|
|
|
+ msg = `fail to post ${action} ${xhr.status}`
|
|
|
}
|
|
|
|
|
|
- const err = new Error(msg);
|
|
|
- err.status = xhr.status;
|
|
|
- err.method = 'post';
|
|
|
- err.url = action;
|
|
|
- return err;
|
|
|
+ const err = new Error(msg)
|
|
|
+ err.status = xhr.status
|
|
|
+ err.method = 'post'
|
|
|
+ err.url = action
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
-function getBodyup(xhr) {
|
|
|
- const text = xhr.responseText || xhr.response;
|
|
|
+function getBodyup (xhr) {
|
|
|
+ const text = xhr.responseText || xhr.response
|
|
|
if (!text) {
|
|
|
- return text;
|
|
|
+ return text
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- return JSON.parse(text);
|
|
|
+ return JSON.parse(text)
|
|
|
} catch (e) {
|
|
|
- return text;
|
|
|
+ return text
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export default function uploadup(option) {
|
|
|
+export default function uploadup (option) {
|
|
|
if (typeof XMLHttpRequest === 'undefined') {
|
|
|
- return;
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- const xhr = new XMLHttpRequest();
|
|
|
- const action = option.action;
|
|
|
+ const xhr = new XMLHttpRequest()
|
|
|
+ const action = option.action
|
|
|
|
|
|
if (xhr.upload) {
|
|
|
- xhr.upload.onprogress = function progress(e) {
|
|
|
+ xhr.upload.onprogress = function progress (e) {
|
|
|
if (e.total > 0) {
|
|
|
- e.percent = e.loaded / e.total * 100;
|
|
|
+ e.percent = e.loaded / e.total * 100
|
|
|
}
|
|
|
- option.onProgress(e);
|
|
|
- };
|
|
|
+ option.onProgress(e)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- const formData = new FormData();
|
|
|
+ const formData = new FormData()
|
|
|
|
|
|
if (option.data) {
|
|
|
Object.keys(option.data).forEach(key => {
|
|
|
- formData.append(key, option.data[key]);
|
|
|
- });
|
|
|
+ formData.append(key, option.data[key])
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
- formData.append(option.filename, option.file);
|
|
|
+ formData.append(option.filename, option.file)
|
|
|
|
|
|
- xhr.onerror = function error(e) {
|
|
|
- option.onError(e);
|
|
|
- };
|
|
|
+ xhr.onerror = function error (e) {
|
|
|
+ option.onError(e)
|
|
|
+ }
|
|
|
|
|
|
- xhr.onload = function onload() {
|
|
|
+ xhr.onload = function onload () {
|
|
|
if (xhr.status < 200 || xhr.status >= 300) {
|
|
|
- return option.onError(getErrorup(action, option, xhr));
|
|
|
+ return option.onError(getErrorup(action, option, xhr))
|
|
|
}
|
|
|
|
|
|
- option.onSuccess(getBodyup(xhr));
|
|
|
- };
|
|
|
+ option.onSuccess(getBodyup(xhr))
|
|
|
+ }
|
|
|
|
|
|
- xhr.open('post', action, true);
|
|
|
+ xhr.open('post', action, true)
|
|
|
|
|
|
if (option.withCredentials && 'withCredentials' in xhr) {
|
|
|
- xhr.withCredentials = true;
|
|
|
+ xhr.withCredentials = true
|
|
|
}
|
|
|
|
|
|
- const headers = option.headers || {};
|
|
|
+ const headers = option.headers || {}
|
|
|
|
|
|
for (let item in headers) {
|
|
|
if (headers.hasOwnProperty(item) && headers[item] !== null) {
|
|
|
- xhr.setRequestHeader(item, headers[item]);
|
|
|
+ xhr.setRequestHeader(item, headers[item])
|
|
|
}
|
|
|
}
|
|
|
- xhr.send(formData);
|
|
|
- return xhr;
|
|
|
+ xhr.send(formData)
|
|
|
+ return xhr
|
|
|
}
|