【问题标题】:Uploading files via jQuery, object FormData is provided and no file name, GET request通过jQuery上传文件,提供对象FormData并且没有文件名,GET请求
【发布时间】:2013-12-25 12:19:18
【问题描述】:

我正在使用 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;
});

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://localhost:4965/test/file_capture?[object%20FormData]&_=1386501633340

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

【问题讨论】:

标签: jquery forms


【解决方案1】:

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

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>

【讨论】:

  • 尝试编辑的版本 - 我添加了 preventDefault() 并设置了“类型”
  • 还是同样的问题,同样的 URL!
  • 如何在后端捕获文件?我是说下一页?我正在使用 ASP.NET..
  • @marv 如果我只有图像源而不是输入文件中的图像,我可以在哪里上传图像?
  • 不,它是 JavaScript。您应该使用后端 php/ruby 来下载远程图像
【解决方案2】:

您需要显式获取文件。

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

【讨论】:

    【解决方案3】:

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

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

    另外,you need HTML 5 to be able to upload files(尽管 Firefox 可能允许在早期的 XHTML 中使用它)。

    【讨论】:

    • 好的,那么我将如何捕获下一页上的文件?它将被分配到什么名称?
    • 我知道表单是如何发送的,但我想知道我的表单元素没有在那里捕获!我使用了这个Request["image"](asp.net razor),但是if(image == null)被触发的块和其他块,如果图像被捕获就会执行。
    • 我不知道 ASP.NET 如何处理文件上传,抱歉。您应该有办法获取文件,但是,许多库处理上传文件的方式与其他表单元素不同。查看the answer for a similar thread,我注意到文件似乎在Request.Files 数组中。
    【解决方案4】:

    首先获取对象

    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
    })
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 2022-01-06
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2013-11-10
      相关资源
      最近更新 更多