简体   繁体   English

如何使用 multipart/form-data 发送请求?

[英]how do I send a request using multipart/form-data?

I have an app in ReactJs, using Axios and Papaparse.我在 ReactJs 中有一个应用程序,使用 Axios 和 Papaparse。

I have a page where a user drop a csv file in a box, I automatically download the csv, update and make some change in the data, and send a new csv file to a server.我有一个页面,用户将一个 csv 文件放入一个盒子中,我自动下载 csv,更新并更改数据,然后将新的 csv 文件发送到服务器。

I did all until I arrive to the part where I need to create a new csv, and upload it to the server.我完成了所有工作,直到到达需要创建新 csv 并将其上传到服务器的部分。

Here is my code currently :这是我目前的代码:

            const data = papaparse.unparse(destinationUpdateData, {
                header: true,
                skipEmptyLines: true
            });
            // data is a string in csv format

            const file = new File([data as BlobPart], "destination.csv",  { type: "text/csv" });
            // I get a File type.

            const paramsDestination = {
                project_id: this.props.projectId,
                datastore_id: 'DESTINATIONS',
                file: file,
                id: ["1", "2","3"]
            }
            // the data I would like to send is build

            Axios.post(`new_item_file_attachment`, params, {headers: {"Content-Type": "multipart/form-data"}})
            //I send to the server

The thing is, my server is expecting a request with a content Type of multipart/form-data , but I don't get how manually set my parameter to match this type.问题是,我的服务器正在等待内容类型为multipart/form-data的请求,但我不知道如何手动设置我的参数以匹配此类型。

The api call currently don't work, because the data is passed like a json, and the server reject it. api 调用当前不起作用,因为数据像 json 一样传递,服务器拒绝它。

Is it possible to achieve it ?有可能实现吗?

I tried using FormData , but I can't see how to send boolean and array我尝试使用FormData ,但我看不到如何发送布尔值和数组

Not 100% familiar with Axios but it should be something like this:不是 100% 熟悉 Axios,但它应该是这样的:

var params = new FormData();

params.append("project_id", this.props.projectId);
params.append("datastore_id", 'DESTINATIONS');
params.append("file", file);
params.append("id", JSON.stringify(["1", "2","3"])); // Arrays must be stringified
Axios.post(`new_item_file_attachment`, params)

You definitely need to put everything in FormData object.您肯定需要将所有内容都放在 FormData 对象中。 Last time I was doing this, I also had to remove the "Content-Type": "multipart/form-data" from the header.上次我这样做时,我还必须从标题中删除“Content-Type”:“multipart/form-data”。 I believe the correct header should get filled in automatically.我相信应该自动填充正确的标题。 Try it with and without that stating a header and let me know if either works.尝试使用和不使用声明标题并让我知道其中任何一个是否有效。

Here is my solution.这是我的解决方案。

const data = new FormData();
data.append("project_id", id);
data.append("file", file);

axios.post(url, data);

Try and comments when some errors occur.出现一些错误时尝试和评论。

暂无
暂无

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

相关问题 使用`fetch`或`request`发送多部分/表单数据的正确方法 - Proper way to send multipart/form-data using `fetch` or `request` 使用qunit的多部分/表单数据请求 - multipart/form-data request using qunit 将收到的文件发送到 axios multipart/form-data 请求 - Send received file to an axios multipart/form-data request 我如何在angularjs中使用$ http.post()在post方法中使用enctype =“ multipart / form-data”发送表单数据 - how can i send the form data with enctype=“multipart/form-data” in the post method using $http.post() in angularjs 如何使用 vue 在一个请求中发送 json 参数和 multipart/form-data 文件 - How to send json parameter and multipart/form-data file in one request with vue 如何在多部分/表单数据请求中发送对象而不在Angular中转换为字符串 - How to send Object in a multipart/form-data request without converting to string in Angular 如何通过ajax(没有jquery)发送multipart / form-data表单内容? - How to send multipart/form-data form content by ajax (no jquery)? 如何在jQuery POST中将enctype作为multipart / form-data发送 - How to send enctype as multipart/form-data in jquery POST 如何使用AngularJS $ http发送multipart / form-data - How to use AngularJS $http to send multipart/form-data Javascript将图像作为URI数据,如何通过multipart / form-data表单上传此图像? - Javascript has an image as URI data, how do I upload this image via a multipart/form-data form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM