如何发送的图像与来自离子HTTP POST的形式Node.js的?(How to send an i

2019-10-28 13:32发布

我想和喜欢的名称和描述在HTTP POST表单数据发送图像。

var body={
 name:name,
 description:desc
}
this.http.post("url",body).subscribe(val=>{
    console.log(val);
})

我怎么可以在HTTP发布数据一起发送的图像?

Answer 1:

要角张贴图片,你需要append它在你这样的表单数据

const formData = new FormData();
formData.append("file", this.angForm.get("image").value);
formData.append("name", this.angForm.get("name").value);
formData.append("description", this.angForm.get("desc").value);

this.http.post("url",formData).subscribe(val=>{
     console.log(val);

});

创建一个函数改变输入值后检查文件

 onFileSelect(event) {
   if (event.target.files.length > 0) {

    const file = event.target.files[0];


    this.form.get("image").setValue(file);
    //here form is your form that you use like reactive form 
   //set form image value
   }


Answer 2:

你可以用做FORMDATA

const formData = new FormData()
formData.append('file', imgBlob, filename)
this.http.post('url', formData)


文章来源: How to send an image with an form in http post from ionic to node.js?