简体   繁体   English

jQuery和php文件上传

[英]jQuery and php file upload

I do not understand, and google does not give me the answer. 我不明白,谷歌也没有给我答案。 I want to upload a file and the results show in the div without page relode, but I can not get it!!!! 我想上传一个文件,结果显示在div中,而页面没有重新布局,但是我无法获取它!

html: HTML:

<form method="post" action="process/pic2.php" enctype="multipart/form-data" id="userpic">
<p>Izvēlies bildi: <input type="file" name="img_to_upload" /><input type="submit" name="upload" value="Augšupielādēt" /></p>
</form>

jquery: jQuery的:

jQuery(function(){
        jQuery('#userpic').submit(function(){
            jQuery.ajax({
                type: 'POST',
                enctype: 'multipart/form-data',
                url: jQuery('#userpic').attr('action'),
                data: jQuery('#userpic').serialize(),
                success: function(data){
                    if(data){
                        jQuery('#picinfo').html(data);
                    }else{
                        jQuery('#uerpic').fadeOut(500);
                        jQuery('#picinfo').html('<center><img src="img/loader.gif" /></center>');
                            window.location = 'index.php';
                    }
                }
            });
            return false;
        });
    });

and my php: 和我的PHP:

if(isset($_FILES['img_to_upload']['name']) && !empty($_FILES['img_to_upload']['name'])){
        echo 'Done!';
    }else{
        echo 'Error!';
    }

all the time showing the "error" text.. 一直显示“错误”文本。

PS Sorry for bad English. PS对不起,英语不好。

Normally, to do a form-submission, and stay on the same page, you'll have to prevent the default form action with Javascript, something like this: 通常,要提交表单并保留在同一页面上,您必须使用Javascript阻止默认的表单操作,如下所示:

$("#userpic").submit(function(event) {
  event.preventDefault();
  ...
});

But, uploading an image via Ajax is pretty tricky--see: 但是,通过Ajax上传图像非常棘手-请参阅:

uploading files with jquery $.ajax and php 用jQuery $ .ajax和php上传文件

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

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