简体   繁体   English

使用 axios 在 POST 多部分/表单数据请求中发送文件和 json

[英]sending file and json in POST multipart/form-data request with axios

I am trying to send a file and some json in the same multipart POST request to my REST endpoint.我正在尝试在同一个多部分 POST 请求中向我的 REST 端点发送一个文件和一些 json。 The request is made directly from javascript using axios library as shown in the method below.该请求是使用 axios 库直接从 javascript 发出的,如下面的方法所示。

doAjaxPost() {
    var formData = new FormData();
    var file = document.querySelector('#file');

    formData.append("file", file.files[0]);
    formData.append("document", documentJson);

    axios({
        method: 'post',
        url: 'http://192.168.1.69:8080/api/files',
        data: formData,
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (response) {
        console.log(response);
    });
}

However, the problem is when I inspect the request in chrome developer tools in the network tab, I find no Content-Type field for document , while for file field Content-Type is application/pdf (I'm sending a pdf file).但是,问题是当我在网络选项卡中的 chrome 开发人员工具中检查请求时,我没有找到document Content-Type字段,而file字段Content-Typeapplication/pdf (我正在发送 pdf 文件)。

网络检查器中显示的请求

On the server Content-Type for document is text/plain;charset=us-ascii .在服务器上, documentContent-Typetext/plain;charset=us-ascii

Update:更新:

I managed to make a correct request via Postman, by sending document as a .json file.通过将document作为.json文件发送,我设法通过 Postman 提出了正确的请求。 Though I discovered this only works on Linux/Mac.虽然我发现这只适用于 Linux/Mac。

To set a content-type you need to pass a file-like object.要设置内容类型,您需要传递一个类似文件的对象。 You can create one using a Blob .您可以使用Blob创建一个。

const obj = {
  hello: "world"
};
const json = JSON.stringify(obj);
const blob = new Blob([json], {
  type: 'application/json'
});
const data = new FormData();
data.append("document", blob);
axios({
  method: 'post',
  url: '/sample',
  data: data,
})

Try this.尝试这个。

doAjaxPost() {
    var formData = new FormData();
    var file = document.querySelector('#file');

    formData.append("file", file.files[0]);
    // formData.append("document", documentJson); instead of this, use the line below.
    formData.append("document", JSON.stringify(documentJson));

    axios({
        method: 'post',
        url: 'http://192.168.1.69:8080/api/files',
        data: formData,
    })
    .then(function (response) {
        console.log(response);
    })
    .catch(function (response) {
        console.log(response);
    });
}

You can decode this stringified JSON in the back-end.您可以在后端解码这个字符串化的 JSON。

you only need to add the right headers to your request您只需要在请求中添加正确的标题

axios({
  method: 'post',
  url: 'http://192.168.1.69:8080/api/files',
  data: formData,
  header: {
            'Accept': 'application/json',
            'Content-Type': 'multipart/form-data',
          },
    })

You cant set content-type to documentJson , because non-file fields must not have a Content-Type header, see HTML 5 spec 4.10.21.8 multipart form data .您不能将 content-type 设置为documentJson ,因为非文件字段不得具有Content-Type header,请参阅HTML 5 spec 4.10.21.8 multipart form data

And there`s two way to achieve your goals:有两种方法可以实现您的目标:

暂无
暂无

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

相关问题 在 Axios vue.js 中发送带有多部分/表单数据发布请求的数组数据 - sending array data with multipart/form-data post request in Axios vue.js 将收到的文件发送到 axios multipart/form-data 请求 - Send received file to an axios multipart/form-data request axios 发布请求正在发送请求 header 的 Content-Type: multipart/form-data 导致未定义的 req.body - axios post request is sending a request header of Content-Type: multipart/form-data resulting in undefined req.body 无法弄清楚如何发出 Axios POST 请求,其中包含 Vue.js 中的“multipart/form-data”文件 - Unable to figure out how to make an Axios POST request which contains “multipart/form-data” file in Vue.js 如何设置POST的MIME类型-axios中的multipart / form-data? - How to set MIME type for POST - multipart/form-data in axios? 要上传文件,在 post body 与 multipart/form-data 中发送 base64 的优缺点是什么 - To upload a file what are the pros and cons of sending base64 in post body vs multipart/form-data 文件上传ajax:您的POST请求的正文格式不正确的multipart / form-data - file upload ajax:The body of your POST request is not well-formed multipart/form-data 我应该如何解码文件的内容以将其包含在 multipart/form-data POST 请求中? - How should I decode the contents of a file to include it in a multipart/form-data POST request? 在Angular2中构建multipart / form-data POST请求并验证输入类型File - Build multipart/form-data POST request in Angular2 and validate Input type File 角度多部分/表单数据表单发送 - Angular multipart/form-data form sending
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM