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

Afz*_*han 5 forms jquery

我正在使用jQuery脚本将文件上传到新页面.它在某种程度上也有效,但问题在于它将表单数据发送为object FormData.

这是代码:

$('#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;
});
Run Code Online (Sandbox Code Playgroud)

HTML部分如下:

<form enctype="multipart/form-data">
  <input type="file" id="image" name="image" accept="Image/*" />
  <input type="submit" id="submit" name="" value="Upload" />
</form>
Run Code Online (Sandbox Code Playgroud)

但生成的链接如下:

HTTP://本地主机:4965 /测试/ file_capture [对象%20FormData] _ = 1386501633340

没有图像名称或附加任何其他东西.我错过了什么?即使没有错误,也会发出请求,并显示上传完成警报.

mar*_*ere 11

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

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

编辑

<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>
Run Code Online (Sandbox Code Playgroud)


Fer*_*sso 7

您需要显式获取该文件.

var image = $('#image')[0].files[0];
Run Code Online (Sandbox Code Playgroud)

然后将文件追加到formData:

formData.append( image );
Run Code Online (Sandbox Code Playgroud)

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

    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.
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)