choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
177 lines (135 loc) • 4.42 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = upload;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _isString = _interopRequireDefault(require("lodash/isString"));
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _isObject = _interopRequireDefault(require("lodash/isObject"));
var _toString = _interopRequireDefault(require("lodash/toString"));
var _isNil = _interopRequireDefault(require("lodash/isNil"));
function getError(option, xhr) {
var msg = "cannot post ".concat(option.action, " ").concat(xhr.status, "'");
var err = new Error(msg);
err.status = xhr.status;
err.method = 'post';
err.url = option.action;
return err;
}
function getBody(xhr) {
var text = xhr.responseText || xhr.response;
if (!text) {
return text;
}
try {
return JSON.parse(text);
} catch (e) {
return text;
}
} // option {
// onProgress: (event: { percent: number }): void,
// onError: (event: Error, body?: Object): void,
// onSuccess: (body: Object): void,
// data: Object,
// filename: String,
// file: File,
// withCredentials: Boolean,
// action: String,
// headers: Object,
// }
function requestFileFormData(requestFileKeys, option) {
var formData = new FormData();
/**
* 添加注入的data数据
*/
if (option.data) {
Object.keys(option.data).map(function (key) {
formData.append(key, option.data[key]);
});
}
/**
* 添加文件数据
*/
formData.append(option.filename, option.file);
var toStringValue = function toStringValue(value) {
if ((0, _isNil["default"])(value)) {
return '';
}
if ((0, _isString["default"])(value)) {
return value;
}
if ((0, _isObject["default"])(value)) {
return JSON.stringify(value);
}
return (0, _toString["default"])(value);
};
/**
*
* @param {所有数据} obj
* @param {需要传出的参数keys} arrayString
*/
var ArrayToObject = function ArrayToObject(obj, arrayString) {
arrayString.forEach(function (item) {
formData.append(toStringValue(item), toStringValue(obj[toStringValue(item)]));
});
};
/**
* 判断是否需要增加key
*/
if ((0, _isString["default"])(requestFileKeys) || (0, _isArray["default"])(requestFileKeys)) {
var requestFileKeysFile = ['uid', 'type', 'name', 'lastModifiedDate'];
if ((0, _isString["default"])(requestFileKeys)) {
requestFileKeysFile.push(requestFileKeys);
} else {
requestFileKeysFile = [].concat((0, _toConsumableArray2["default"])(requestFileKeysFile), (0, _toConsumableArray2["default"])(requestFileKeys));
}
ArrayToObject(option.file, requestFileKeysFile);
}
return formData;
}
function upload(option) {
var xhr = new XMLHttpRequest();
if (option.onProgress && xhr.upload) {
xhr.upload.onprogress = function progress(e) {
if (e.total > 0) {
e.percent = e.loaded / e.total * 100;
}
option.onProgress(e);
};
}
var formData = requestFileFormData(option.requestFileKeys, option);
xhr.onerror = function error(e) {
option.onError(e);
};
xhr.onload = function onload() {
// allow success when 2xx status
// see https://github.com/react-component/upload/issues/34
if (xhr.status < 200 || xhr.status >= 300) {
return option.onError(getError(option, xhr), getBody(xhr));
}
option.onSuccess(getBody(xhr), xhr);
};
xhr.open('post', option.action, true); // Has to be after `.open()`. See https://github.com/enyo/dropzone/issues/179
if (option.withCredentials && 'withCredentials' in xhr) {
xhr.withCredentials = true;
}
var headers = option.headers || {}; // when set headers['X-Requested-With'] = null , can close default XHR header
// see https://github.com/react-component/upload/issues/33
if (headers['X-Requested-With'] !== null) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}
for (var h in headers) {
if (headers.hasOwnProperty(h) && headers[h] !== null) {
xhr.setRequestHeader(h, headers[h]);
}
}
xhr.send(formData);
return {
abort: function abort() {
xhr.abort();
}
};
}
//# sourceMappingURL=request.js.map
;