如何使用jQuery发送文件输入? [英] how to send file input using jquery?

查看:119
本文介绍了如何使用jQuery发送文件输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个文件输入的表单。我想通过jquery.i发送它在jquery中尝试序列化函数,但我意识到这个函数不会发送文件输入!

这里是我的表单:

 < form id =submit_picsaction =#> 

档案1:< input name =file1id =file1type =file/>< br />
档案2:< input name =file2id =file2type =file/>< br />
档案3:< input name =file3id =file3type =file/>< br />

< br />
< div>
< / div>

< / form>

这是我的javascript代码:

 < script type =text / javascript> 
$ b $(document).ready(function(){

$(#level3)。click(function(event){

var str = $(#submit_pics)。serialize();
$ .ajax({
type:POST,
url:track.php,
data:str,
成功:function(theResponse){
$ b $('#req_result')。html(theResponse);

}

return false;
});

});



不幸的是,你不能通过XMLHttpRequest上传文件,AJAX实际上并不会将表单发布到服务器上,它会发送选择的数据以POST或GET请求的形式存在。Javascript无法从用户机器抓取文件并将其发送到服务器。


您可以使用棘手的解决方法,张贴(与out AJAX)请求到一个隐藏的iframe。

看看这个 教程


i have a form that contains 3 File inputs.i want to send it via jquery.i tried serialize function in jquery,but i realized that this function don't send file inputs!

here is my form :

<form id="submit_pics" action="#" >

     file 1 : <input name="file1" id="file1" type="file" /><br />
     file 2 : <input name="file2" id="file2" type="file" /><br />
     file 3 : <input name="file3" id="file3" type="file" /><br />

     <br />
     <div>
         <a id="submit" href="javascript:;" ><img  src="uploads/images/button1.png" /></a>
     </div>

</form>

and this is my javascript code :

<script type="text/javascript">

    $(document).ready(function(){

        $("#level3").click(function(event) {

            var str = $("#submit_pics").serialize(); 
            $.ajax({
                type: "POST",
                url: "track.php",
                data: str, 
                success: function(theResponse) {

                                        $('#req_result').html(theResponse);

                                    }

                return false;   
        });

    });

解决方案

Unfortunately you can not upload files through XMLHttpRequest. AJAX doesn’t actually post forms to the server, it sends selected data to in the form of a POST or GET request. Javascript is not capable of grabbing the file from the users machine and sending it to the server

You can use a tricky workaround, posting (without AJAX) the request to an hidden iframe.

Have a look at this TUTORIAL

这篇关于如何使用jQuery发送文件输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆