与jQuery $就和PHP文件上传 [英] uploading files with jquery $.ajax and php

查看:205
本文介绍了与jQuery $就和PHP文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望上传文件同步,当用户选择一个输入文件中的文件,用$阿贾克斯。但是,recive通话回报指数的PHP的定义。 jQuery的code是下一个:

I desire to upload files asynchronous when the user select a file in a input file, with $.ajax. But the php that recive the call return index undefined. The jquery code is the next:

$("#urlimatge").change(function(){
            var filename = $("#urlimatge").val();
            $.ajax({ 
                type: "POST",
                url: "utils/uploadtempimg.php",
                enctype: 'multipart/form-data',
                data: {'urlimatge' : filename },
                success: function(response){
                     alert(response);
                }
            }); 

        });

和recibe调用PHP的:

and the php that recibe the call:

$image =  new gestorimatges();
$target_path = $image->uploadTemp($_FILES['urlimatge']['name'],$_FILES['urlimatge']['tmp_name']);

感谢

推荐答案

您不能上传文件,AJAX,但您可以使用 IFRAME 让你不要刷新当前页面。

You can't upload files with AJAX, but you can use an iframe so you don't have to refresh the current page.

很多人去海峡插件,但你可以很容易地做到这一点自己pretty的,并与一个AJAX请求中的所有功能。

Many people go strait to plugins but you can do this yourself pretty easily, and with all the functionality of an AJAX request.

而不是使用AJAX功能的,有一个表单提交到隐藏的 IFRAME 具有负荷事件处理程序连接到它,所以当提交表单时,你有一个回调函数,实际上包含了服务器响应(的HTML的 IFRAME 加载后)。

Instead of using an AJAX function, have a form submit to a hidden iframe that has a load event handler attached to it so when the form is submitted, you have a callback function that actually includes the server response (the HTML of the iframe after it loads).

例如:

HTML -

<form action="..." method="post" encrypt="application/x-www-form-urlencoded" target="workFrame" >
    <input type="file" name="file" />
    <input type="submit" />
</form>
<iframe id="workFrame" src="about:blank" style="display:none;"></iframe>

JS -

JS --

$(function () {
    $('form').on('submit', function () {
        //check if the form submission is valid, if so just let it submit
        //otherwise you could call `return false;` to stop the submission
    });

    $('#workFrame').on('load', function () {

        //get the response from the server
        var response = $(this).contents().find('body').html();

        //you can now access the server response in the `response` variable
        //this is the same as the success callback for a jQuery AJAX request
    });
});

这篇关于与jQuery $就和PHP文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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