2013-12-08 67 views
5

我正在使用jQuery脚本将文件上传到新页面。它也可以工作,但问题是它发送表单数据为object FormData通过jQuery,对象上传文件FormData提供,没有文件名,GET请求

下面是代码:

$('#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; 
}); 

HTML部分是:

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

但是所生成的链路是作为:

的http://本地主机:4965/test/file_capture?[object%20FormData] & _ = 1386501633340

哪没有影像电子邮件或任何其他附件。我错过了什么?即使没有错误,并且已提出请求并显示上传完成警报。

+0

可能重复[如何可以与jQuery异步上传文件?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) –

回答

11

只应提交的文件 - 而不是完整的形式

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

编辑

<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> 
+0

我试过了追加这个,但结果相同!没有发送任何内容.. –

+0

尝试编辑版本 - 我添加了preventDefault()和设置'类型' – marvwhere

+0

仍然是相同的问题,相同的URL! –

7

你需要明确获得文件。

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

然后追加该文件FORMDATA:

formData.append(image); 

这里是我如何做到这一点的例子:

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. 
      } 
     }); 
    } 
} 
+0

不起作用,它仍然只发送FormData对象!没有别的。 –

0

文件不能与上传GET方法。您需要使用POST

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

此外,you need HTML 5 to be able to upload files(虽然Firefox可能允许它与早期的XHTML)。

+0

好的,那么我将如何捕获下一页上的文件?它将分配什么名字? –

+0

http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work –

+0

我知道如何发送表单,但我想知道我的表单元素没有捕获到那里!我使用了这个'Request [“image”]'(asp.net razor),但是if(image == null)'被触发的块和其他块,如果捕获图像将被执行,将被忽略。 –