问题描述
我正试图通过主要改装一些现有的js将二进制文件发送到API。
现有项目与我的项目之间的巨大区别:
使用Node导入二进制文件并发送到API(我需要)
与
从浏览器/ html上传二进制文件并发送到API(现有)
我使用fs.readFile()的努力被证明是失败的。 在检查现有脚本(下面的链接)时,在发送到API之前文件没有什么特别的事情。
您可以在这里查看现有的代码/脚本:https://api.pvpgn.pro/example/d2edit/ https://github.com/pvpgn/api.pvpgn.pro/blob/master/WebAPI/wwwroot/example/d2edit/index.html
// Main culprit///
async function readSave() {
fs.readFile('asdgasdgasdg.d2s',async function read(err,data) {
if (err)
throw err;
if (data){
uploadFile(data,'charinfo')
}
})
}
//// CODE BELOW WORKS WITH FILE UPLOAD FROM WEBPAGE /////
function uploadFile(file,type) {
var xhr = new XMLHttpRequest();
var formData = new FormData();
xhr.open('POST',url,true);
xhr.addEventListener('readystatechange',function (e) {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// Done. Inform the user
var json = JSON.parse(xhr.responseText);
console.log(json);
if (json.result === "error") {
if (type === 'charinfo') {
// try the same file with charsave type
uploadFile(file,'charsave');
} else {
return throwError(json.errorMessage);
}
} else {
charObjects.push(json.data);
var idx = charObjects.length - 1; // get last element index
charObjects[idx].idx = idx; // add new field with index
if (json.data.fileType !== 'charinfo' && json.data.fileType !== 'charsave') {
return throwError("Unsupported file type"); // charitem
}
}
} else if (xhr.readyState === XMLHttpRequest.DONE && xhr.status !== 200) {
// Error. Inform the user
console.log("error");
}
})
formData.append('file',file);
xhr.send(formData)
xhr.onload = function() {
// See response incase it failed...
console.log(xhr)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)