繁体   English   中英

使用nodejs将图像上传到预签名的url

[英]Uploading image to presigned url using nodejs

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` `

这是工作代码。

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);

对于这种方法,您无需创建表单数据。 只需获取文件对象并上传即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM