UNPKG

3.49 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4exports.default = upload;
5/**
6 * clone from https://github.com/react-component/upload/blob/master/src/request.js
7 */
8
9function getError(option, xhr, msg) {
10 msg = msg || 'cannot post ' + option.action + ' ' + xhr.status + '\'';
11 var err = new Error(msg);
12 err.status = xhr.status;
13 err.method = option.method;
14 err.url = option.action;
15 return err;
16}
17
18function getBody(xhr) {
19 var text = xhr.responseText || xhr.response;
20 if (!text) {
21 return text;
22 }
23
24 try {
25 return JSON.parse(text);
26 } catch (e) {
27 return text;
28 }
29}
30
31// option {
32// onProgress: (event: { percent: number }): void,
33// onError: (event: Error, body?: Object): void,
34// onSuccess: (body: Object): void,
35// data: Object,
36// filename: String,
37// file: File,
38// withCredentials: Boolean,
39// action: String,
40// headers: Object,
41// method: String
42// timeout: Number
43// }
44function upload(option) {
45 var xhr = new XMLHttpRequest();
46
47 if (option.onProgress && xhr.upload) {
48 xhr.upload.onprogress = function progress(e) {
49 if (e.total > 0) {
50 e.percent = e.loaded / e.total * 100;
51 }
52 option.onProgress(e);
53 };
54 }
55
56 var formData = new FormData();
57
58 if (option.data) {
59 Object.keys(option.data).forEach(function (key) {
60 formData.append(key, option.data[key]);
61 });
62 }
63 if (option.file instanceof Blob) {
64 formData.append(option.filename, option.file, option.file.name);
65 } else {
66 formData.append(option.filename, option.file);
67 }
68
69 xhr.onerror = function error(e) {
70 option.onError(e);
71 };
72
73 xhr.onload = function onload() {
74 // allow success when 2xx status
75 // see https://github.com/react-component/upload/issues/34
76 if (xhr.status < 200 || xhr.status >= 300) {
77 return option.onError(getError(option, xhr), getBody(xhr));
78 }
79
80 option.onSuccess(getBody(xhr), xhr);
81 };
82
83 option.method = option.method || 'POST';
84 xhr.open(option.method, option.action, true);
85
86 // In Internet Explorer, the timeout property may be set only after calling the open() method and before calling the send() method.
87 // see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout
88 var timeout = option.timeout;
89
90
91 if (typeof timeout === 'number' && timeout > 0) {
92 xhr.timeout = timeout;
93 xhr.ontimeout = function () {
94 var msg = 'Upload abort for exceeding time (timeout: ' + timeout + 'ms)';
95 option.onError(getError(option, xhr, msg), getBody(xhr));
96 };
97 }
98
99 // Has to be after `.open()`. See https://github.com/enyo/dropzone/issues/179
100 if (option.withCredentials && 'withCredentials' in xhr) {
101 xhr.withCredentials = true;
102 }
103
104 var headers = option.headers || {};
105
106 // when set headers['X-Requested-With'] = null , can close default XHR header
107 // see https://github.com/react-component/upload/issues/33
108 if (headers['X-Requested-With'] !== null) {
109 xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
110 }
111
112 for (var h in headers) {
113 if (headers.hasOwnProperty(h) && headers[h] !== null) {
114 xhr.setRequestHeader(h, headers[h]);
115 }
116 }
117 xhr.send(formData);
118
119 return {
120 abort: function abort() {
121 xhr.abort();
122 }
123 };
124}
125module.exports = exports['default'];
\No newline at end of file