简体   繁体   English

文件上传ajax:您的POST请求的正文格式不正确的multipart / form-data

[英]file upload ajax:The body of your POST request is not well-formed multipart/form-data

I am trying to upload a file to s3 with direct browser upload using ajax 我正在尝试使用Ajax通过直接浏览器上传将文件上传到s3

Here is my html 这是我的html

<form id="upload" action="//s3-ap-south-1.amazonaws.com/cushbudirect" method="POST" enctype="multipart/form-data">

    <input type="hidden" name="Content-Type" value="multipart/form-data" />
<input type="hidden" name="acl" value="private" />
<input type="hidden" name="success_action_status" value="201" />
<input type="hidden" name="policy" value="eyJleHBpcmF0aW9uIjoiMjAxNy0wMy0yMVQxNDoxMTozMFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJjdXNoYnVkaXJlY3QifSx7ImFjbCI6InByaXZhdGUifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIiJdLFsiZXEiLCIkQ29udGVudC1UeXBlIiwibXVsdGlwYXJ0XC9mb3JtLWRhdGEiXSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDUyNDI4ODAwMF0seyJzdWNjZXNzX2FjdGlvbl9zdGF0dXMiOiIyMDEifSx7IngtYW16LWNyZWRlbnRpYWwiOiJBS0lBSlJWRVhVVFRWTjROWUFSUVwvMjAxNzAzMjFcL2FwLXNvdXRoLTFcL3MzXC9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDE3MDMyMVQwODExMzBaIn1dfQ==" />
<input type="hidden" name="X-amz-credential" value="AKIAJRVEXUTTVN4NYAQ/20170321/ap-south-1/s3/aws4_request" />
<input type="hidden" name="X-amz-algorithm" value="AWS4-HMAC-SHA256" />
<input type="hidden" name="X-amz-date" value="20170321T081130Z" />
<input type="hidden" name="X-amz-signature" value="152c81d87fc8bbf2642e26b28f1c41b361e7285e835971b10c74ee6d7f03b5" />
<input type="hidden" name="key" value="hello.jpg" />
    <input type="file" name="file" id="image">
    <input type="submit" value="upload" name="upload">
</form>

Js JS

$('#upload').submit(function (e) {
            e.preventDefault();
            //get form data
            var  data= new FormData($("#upload")[0]);
            console.log(JSON.stringify(data));
            $.ajax({
                url:$(this).attr('action'),
                method:"post",
                processData:false,
                data:image,
                contentType:'multipart/form-data',
                success:function (data) {
                   console.log(JSON.stringify(data));
                },
                error:function (err) {
                    console.log(JSON.stringify(err));
                }
            })
        });

But i got the following error from the aws xml error response . 但是我从aws xml错误响应中收到以下错误。

The body of your POST request is not well-formed multipart/form-data. 您的POST请求主体不是格式正确的multipart / form-data。

I've tried the solutions from similar questions but none of them worked for me. 我曾尝试过类似问题的解决方案,但没有一个对我有用。

However when i try to upload without ajax(Normal formsubmit) the uploads works fine, So i am sure about its the problem with form data 但是,当我尝试不使用ajax(Normal formsubmit)上传时,上传效果很好,所以我可以确定其与form data有关的问题

console.log(JSON.stringify(data)); returns {} . 返回{}

Try to change your ajax function as given below and let me know if it works. 尝试按如下所示更改ajax函数,让我知道它是否有效。

EDIT: 编辑:

$('#upload').submit(function (e) {
            e.preventDefault();
            //get form data
            var  data = new FormData($("#upload")[0]);
            console.log(JSON.stringify(data));
            $.ajax({
                url:$(this).attr('action'),
                type : 'PUT',
                processData:false,
                data:data,
                contentType:data.type,
                success:function (data) {
                   console.log(JSON.stringify(data));
                },
                error:function (err) {
                    console.log(JSON.stringify(err));
                }
            })
        });

暂无
暂无

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

相关问题 使用签名和策略返回正文的 Google 应用脚本将图像上传到 Amazon S3 格式不正确 multipart/form-data - Google app script upload image to Amazon S3 using signature and policy return body is not well-formed multipart/form-data 要上传文件,在 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 Request.js模拟文件上传(多部分/表单数据) - Request.js Simulate a File Upload (multipart/form-data) 使用 axios 在 POST 多部分/表单数据请求中发送文件和 json - sending file and json in POST multipart/form-data request with axios Javascript / AJAX POST多部分/表单数据 - Javascript/AJAX POST multipart/form-data 为什么ajax上传文件不需要form标签中的enctype=&quot;multipart/form-data&quot;? - Why ajax upload file doesn't need enctype=“multipart/form-data” in the form tag? 通过 Safari 中的 multipart/form-data 发布的文件上传服务工作人员未触发“获取”事件侦听器 - "fetch" event listener not triggering in service worker for file upload via multipart/form-data post in Safari 使用multipart / form-data的Ajax Post dataURL - Ajax Post dataURL using multipart/form-data 是否可以通过jquery ajax调用伪造multipart / form-data帖子? - Is it possible to fake a multipart/form-data post with a jquery ajax call? jQuery AJAX从文件中检索SVG-错误:格式不正确 - jQuery AJAX retrieve SVG from file - ERROR: 'not well-formed'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM