【问题标题】:How to send an image with an form in http post from ionic to node.js?如何将带有表单的图像从 ionic 发送到 node.js?
【发布时间】:2019-10-18 00:51:58
【问题描述】:

我想在 http post 中发送带有名称和描述等表单数据的图像。

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

如何在 HTTP post 中发送图像和数据?

【问题讨论】:

    标签: node.js http ionic-framework


    【解决方案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
       }
    

    【讨论】:

      【解决方案2】:

      您可以使用FormData 完成此操作

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

      【讨论】:

      • html部分会有变化吗?
      • 我用 FormData 得到了它。
      猜你喜欢
      • 2017-04-08
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多