jQuery ajax 发布文件字段

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

jQuery ajax post file field

jqueryajax

提问by geoffs3310

I have a form with a file input. How do I get the file and post it to a php script using jQuery? Can I just use .val() to get the value and then post this? For example say the file input has an id of file could i just do:

我有一个带有文件输入的表单。如何获取文件并将其发布到使用 jQuery 的 php 脚本?我可以只使用 .val() 来获取值然后发布这个吗?例如,说文件输入有一个文件 id,我可以这样做:

$('#submit').click(function() {
    var file = $('#file').val();
    $.post('script.php', { "file": file }, function(data) {
        // do something here after post complete
    }, 'json');
});

Thanks

谢谢

回答by austinbv

This should help. How can I upload files asynchronously?

这应该有帮助。如何异步上传文件?

As the post suggest I recommend a plugin located here http://malsup.com/jquery/form/#code-samples

正如帖子所建议的,我推荐一个位于http://malsup.com/jquery/form/#code-samples的插件

回答by WhiteHorse

I tried this code to accept files using Ajax and on submit file gets store using my php file. Code modified slightly to work. (Uploaded Files: PDF,JPG)

我尝试使用此代码使用 Ajax 接受文件,并在提交文件时使用我的 php 文件存储。代码稍加修改即可工作。(上传文件:PDF、JPG)

function verify1() {
    jQuery.ajax({
        type: 'POST',
        url:"functions.php",
        data: new FormData($("#infoForm1")[0]),
        processData: false, 
        contentType: false, 
        success: function(returnval) {
             $("#show1").html(returnval);
             $('#show1').show();
         }
    });
}

Just print the file details and check. You will get Output. If error let me know.

只需打印文件详细信息并检查即可。你会得到输出。如果错误让我知道。

回答by Dutchie432

File uploads can notbe done this way, no matter how you break it down. If you want to do an ajax/async upload, I would suggest looking into something like Uploadify, or Valums

无论您如何分解,文件上传都无法通过这种方式完成。如果您想做 ajax/async 上传,我建议您查看UploadifyValums 之类的内容

回答by Herik Modi

Try this...

尝试这个...

<script type="text/javascript">
      $("#form_oferta").submit(function(event) 
      {
           var myData = $( form ).serialize(); 
           $.ajax({
                type: "POST", 
                contentType:attr( "enctype", "multipart/form-data" ),
                url: " URL Goes Here ",  
                data: myData,  
                success: function( data )  
                {
                     alert( data );
                }
           });
           return false;  
      });
</script>

Here the contentTypeis specified as multipart/form-dataas we do in the form tag, this will work to upload simple file On server side you just need to write simple file upload code to handle this request with echoing message you want to show to user as a response.

这里的contentType指定与multipart/form-data我们在表单标签中所做的一样,这将用于上传简单文件