简体   繁体   English

如何使用jQuery以常规格式传递文件

[英]How to pass files with jquery on regular form

I got this code, i would need to pass $_FILES to mail.php from /form form page/ is not passed with this type of jquery, anyone ? 我得到了这段代码,我需要将$ _FILES从/ form表单页面/传递到mail.php,而没有通过这种类型的jquery传递,有人吗?

    function send(datastr){
    $.ajax({    
        type: "POST",
        url: "mail.php",
    data: datastr,
        cache: false,
        success: function(html){
        $("#risposta").fadeIn("slow");
        $("#risposta").css("padding","20px");
        $("#risposta").css("height","auto");
        $("#risposta").css("border","1px solid #c98b18");
        $("#risposta").html(html);
        setTimeout('$("#risposta").fadeOut("slow")',2000);
    }
    });
}

I've yet to see a way to do this with AJAX. 我还没有找到使用AJAX做到这一点的方法。 ATM, you will have to generate a form on-the-fly. 在ATM上,您必须即时生成表格。 The only problem with that is the user will have to select the file that you want to upload, and then from there you can continue automation. 唯一的问题是用户将不得不选择要上传的文件,然后从那里可以继续自动化。

I believe you cannot do this because ajax has no manipulation access to the files system of the server. 我相信您不能这样做,因为ajax无法访问服务器的文件系统。 It can only load data from server but can't manipulate the files in the server side (I guess, I think I read that somewhere). 它只能从服务器加载数据,而不能在服务器端处理文件(我想,我想我在某处读过)。

However you can fake it using a hidden iframe. 但是,您可以使用隐藏的iframe伪造它。 When you submit the form the information goes to the iframe which manipulate the files system of the server. 当您提交表单时,信息将进入iframe,该iframe操纵服务器的文件系统。 Here's the tutorial: http://www.openjs.com/articles/ajax/ajax_file_upload/ 这是教程: http : //www.openjs.com/articles/ajax/ajax_file_upload/

You will notice that the page does load more data when you submit the form, but the page itself isn't reloaded. 您会注意到,提交表单时页面确实加载了更多数据,但是页面本身并未重新加载。

There are also some plugins. 也有一些插件。 The most known is jQuery Form Plugin . 最著名的是jQuery Form Plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM