【问题标题】:Passing file and object to body in Post在 Post 中将文件和对象传递给正文
【发布时间】:2019-10-01 14:50:12
【问题描述】:

我获得了 blob url,并且能够为每个 blob url 创建一个文件并将它们发送到后端,但是,我还需要传递一堆不同对象的属性。 (这些属性与文件完全无关)

问题是我无法将它们一起发送到后端。当然,我尝试只发送一个对象并将我的表单数据(文件)作为对象属性传递,但它不起作用。我也试过 content-type: multipart/form-data。

请不要担心语法错误。

export const sendObjectAndFile = (myObj, file) => {
 return function(dispatch) {
    let form = new FormData();
    form.append('pdf file', file)
    return fetch(URL, {
        method: "POST",
        credentials: 'include',
        body: myObj /* If I pass only form or only myObj that works, however, how can I send them together */
    }).then(response => 
        if (response.success) {
        } else {
        }
    });
}

为了了解我如何处理支持的 blob url(我假设我只传递了文件)

var myFile = Request.Files;

【问题讨论】:

    标签: c# reactjs post upload


    【解决方案1】:

    你可以试试 formData.append(${property name}, object),然后传递整个表单数据。

    例如。

    let formData = new FormData();
    formData.append("myFile", $("#file")[0].files[0]);
    formData.append("title", $("#title").val();
    

    然后在C#控制器中

    public void Post(HttpPostedFileBase myFile, string title){
    // implementation here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2016-02-18
      • 2020-03-21
      相关资源
      最近更新 更多