当输入是文件时,使用 Jquery AJAX 发布表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4597487/
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-26 17:32:17  来源:igfitidea点击:

POST a form with Jquery AJAX When input is a File

jquerypost

提问by tpow

I have several areas where I use AJAX to submit text fields.

我有几个领域使用 AJAX 提交文本字段。

 var name = $("input#name").val(); 

   $.ajax({  
      type: "POST",  
      url: "bin/process.php",  
      data: "&name=" + name,  
      success: function() {  
       //handle response here
     }  
    }); 

However, I can't figure out how to do this if the input type is a file? Doing

但是,如果输入类型是文件,我无法弄清楚如何执行此操作?正在做

var name = $("input#file").val();doesn't seem to work..

var name = $("input#file").val();好像不行。。

采纳答案by Rocket Hazmat

By default, jQuery cannot POST a form via AJAX if it contains a upload field.

默认情况下,如果表单包含上传字段,jQuery 无法通过 AJAX POST 表单。

You can try this plugin: http://jquery.malsup.com/form/

你可以试试这个插件:http: //jquery.malsup.com/form/

回答by RDL

This is typically done using an iFrame to send the file to the server.

这通常是使用 iFrame 将文件发送到服务器来完成的。

The jQuery Form Pluginis a great resource for that.

jQuery的表格插件是一个很好的资源。

Here is another upload scriptwhich uses xhr when possible (FF, Safari) and falls back gracefully to iFrame if necessary (IE)

这是另一个上传脚本,它在可能的情况下使用 xhr(FF、Safari)并在必要时优雅地回退到 iFrame(IE)