jQuery 通过jQuery上传文件,提供了对象FormData,没有文件名,GET请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20452853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:10:31  来源:igfitidea点击:

Uploading files via jQuery, object FormData is provided and no file name, GET request

jqueryforms

提问by Afzaal Ahmad Zeeshan

I am using a jQuery script to upload files to a new page. It somehow works too, but the issue is that it sends the form data as object FormData.

我正在使用 jQuery 脚本将文件上传到新页面。它以某种方式也有效,但问题是它将表单数据作为object FormData.

Here is the code:

这是代码:

$('#submit').click(function () {
   var formData = new FormData($(this).form);
   $.ajax({
       url: '/test/file_capture',
       //Ajax events
       beforeSend: function (e) { 
         alert('Are you sure you want to upload document.'); 
       },
       success: function (e) { 
         alert('Upload completed'); 
       },
       error: function (e) { 
         alert('error ' + e.message); 
       },
       // Form data
       data: formData,
       //Options to tell jQuery not to process data or worry about content-type.
       cache: false,
       contentType: false,
       processData: false
    });
    return false;
});

The HTML part is as:

HTML 部分如下:

<form enctype="multipart/form-data">
  <input type="file" id="image" name="image" accept="Image/*" />
  <input type="submit" id="submit" name="" value="Upload" />
</form>

But the link that is generated is as:

但是生成的链接是:

http://localhost:4965/test/file_capture?[object%20FormData]&_=1386501633340

http://localhost:4965/test/file_capture?[object%20FormData]&_=1386501633340

Which has no image name or any other thing attached to it. What am I missing? Even though there is no error and the request is made and the Upload complete alert is shown.

它没有图像名称或任何其他附加内容。我错过了什么?即使没有错误并且发出请求并显示上传完成警报。

回答by marvwhere

you should only submit the file - not the complete form

您应该只提交文件 - 而不是完整的表格

var fileInput = $('#image');
var file = fileInput.files[0];
var formData = new FormData();
formData.append('file', file);

EDIT

编辑

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<form enctype="multipart/form-data">
  <input type="file" id="image" name="image" accept="Image/*" />
  <input type="submit" id="submit" name="" value="Upload" />
</form>

<script>
$('#submit').click(function (event) {
    event.preventDefault()
   var file = $('#image').get(0).files[0];
   var formData = new FormData();
   formData.append('file', file);
   $.ajax({
       url: '/test/file_capture',
       //Ajax events
       beforeSend: function (e) {
         alert('Are you sure you want to upload document.');
       },
       success: function (e) {
         alert('Upload completed');
       },
       error: function (e) {
         alert('error ' + e.message);
       },
       // Form data
       data: formData,
       type: 'POST',
       //Options to tell jQuery not to process data or worry about content-type.
       cache: false,
       contentType: false,
       processData: false
    });
    return false;
});
</script>

回答by Fernando Basso

You need to explicitly get the file.

您需要明确获取该文件。

var image = $('#image')[0].files[0];

And then append the file to formData:

然后将文件附加到 formData:

formData.append( image );

Here's an example of how I do it:

这是我如何做的一个例子:

    var image = $('#image')[0].files[0];

    if( window.FormData ) {
        formdata = new FormData();
        formdata.append( 'image', image );
        formdata.append( 'action', 'save-image' );

        $.ajax({
            url: 'controller/handler',
            type: 'POST',
            data: formdata,
            processData: false,
            contentType: false,
            success: function( res ) {
                // Handle it.
            }
        });
    }
}

回答by Mike DeSimone

Files cannot be uploaded with the GETmethod. You need to use POST.

无法使用GET方法上传文件。您需要使用POST

$.ajax({
   method: 'POST',
   url: '/test/file_capture',
   // ...

Also, you need HTML 5 to be able to upload files(though Firefox might allow it with earlier XHTML).

此外,您需要 HTML 5 才能上传文件(尽管 Firefox 可能允许使用早期的 XHTML)。

回答by Emanovwe Emmanuel J

First get the Object

首先获取对象

First get the Object from HTML

 //HTML
 <input id = "file_name" type = "file" />

 //JS
 var formData = new FormData()
 var file_obj = document.getElementById("file_name")
 formData.append('file_name', file_obj.files[0]);
 $.ajax({
    url: url,
    type: 'POST',
    data: formData,
    success: function (data) {

    },
    cache: false,
    contentType: false,
    processData: false
})