I am trying to upload a image to url using xmlhttprequest module in nodejs.For that I need to append fields to form data.
const formData = new FormData();
//op.data->presignedData
Object.keys(op.data.fields).forEach(key => {
formData.append(key, op.data.fields[key]);
});
formData.append("webmasterfile", b);//`b` is my file
xhr.open("POST", op.data.url,true);
error:
TypeError:source.on不是函数 在Function.DelayedStream.create(D:\ upload \ node_modules \ delayed-stream \ lib \ delayed_stream.js:33:10)
//Reason :The keys that I am appending should be stringify but How to do it or any other method.
` const xhr = new XMLHttpRequest();
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var b = new Blob([content], { type: "text/xml"});
xhr.open('PUT', op.data.url);
//sending file``
xhr.send(b);`enter code here` `
答案 0 :(得分:0)
这是工作代码。
var file=event.target.files[0]
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function (evt) {
if (evt.lengthComputable) {
progressCb(Math.round(evt.loaded * 100 / evt.total));
} else {
progressUpdateCb(0)
}
}, false);
xhr.addEventListener('load', function () {
sucessCb()
}, false);
xhr.addEventListener('error', function () {
errorCb()
}, false);
xhr.open('PUT', url);
//sending file
xhr.send(file);
对于这种方法,您无需创建表单数据。只需获取文件对象并上传即可。