获取ExcelBlob流
$.ajax({
url:excelUrl,
responseType: 'arraybuffer',
success:function(data){
var file = new File([data],"测试文件.xlsx" ,{type: ".xlsx", lastModified: Date.now()});
console.log(file);
let fileUid = uploadDatasetFile(file);
console.log(fileUid)
},
error:function(message){
console.log(message)
}
});
获取token、提交数据集
let submitDatasetFile = function (data) {
soaSvc.postUnchecked("Core-2006-03-FileManagement", "commitDatasetFiles", {
commitInput: data
}).then(function (result) {})
}let getXSRFToken = function () {
let token = '';
if (document !== null && document !== undefined) {
if (document.cookie.search('XSRF-TOKEN') > -1) {
const splitAtr = document.cookie.split('XSRF-TOKEN=');
if (splitAtr.length === 2) {
// returns the first element
token = splitAtr[1].split(';')[0];
}
}
}
return token;
}
数据集对象创建与绑定
let uploadDatasetFile = function (file) {
return new Promise(function (resolve, reject) {
let fileType = file.name.split(".")[1];let getObj = checkTypes(fileType);
soaSvc.postUnchecked("Core-2010-04-DataManagement", "createDatasets", {
input: [{
attrs: [],
clientId: file.name,
container: { uid: "AAAAAAAAAAAAAA", type: "unknownType" },
datasetFileInfos: [{
allowReplace: false,
clientId: "",
fileName: file.name,
isText: false,
namedReferenceName: getObj.names
}],
datasetId: "",
datasetRev: "",
description: "",
name: file.name,
nrObjectInfos: [],
relationType: "",
toolUsed: "",
type: getObj.type
}],
}).then(function (result) {
let datasetUID = result.ServiceData.created[0]; //数据集UID
console.log(datasetUID);
var input = [{
clientId: "",
primaryObject: {
uid: "A_vNB8VW5pmj_C",
type: "Folder"},
relationType: "",
secondaryObject: {
uid: datasetUID,
type: "MSExcelX"
},
userData: {
uid: "AAAAAAAAAAAAAA",
type: "unknownType"
},
}];
soaSvc.post('Core-2006-03-DataManagement','createRelations',{"input":input} ).then(async function(response){
console.log(response);
});
let ticket = result.datasetOutput[0].commitInfo[0].datasetFileTicketInfos[0].ticket;
let datasetFileInfo = result.datasetOutput[0].commitInfo[0].datasetFileTicketInfos[0].datasetFileInfo;
datasetFileInfo.namedReferencedName = getObj.names;
let dataset = result.datasetOutput[0].dataset;
let commitInput = [{
createNewVersion: true,
dataset: dataset,
datasetFileTicketInfos: [{
datasetFileInfo: datasetFileInfo,
ticket: ticket
}]
}];submitDatasetFile(commitInput);
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', browserUtils.getBaseURL() + 'fms/fmsupload/', true);
httpRequest.setRequestHeader('X-XSRF-TOKEN', getXSRFToken());
var formData = new FormData();formData.append('fmsFile', file, file.name);
formData.append('fmsTicket', ticket);httpRequest.send(formData);
resolve({
datasetUID: datasetUID
})
});})
};