duanlian1320 2014-08-04 12:36
浏览 50
已采纳

Ajax和PHP上传文件[关闭]

I'm starting with web programming with javascript and i have a little problem with uploading file.

I find this: http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/ but I have no idea how to use upload.php file in mvc project. What to use as an action of form and where should be plasted this php code?

Can anyone give mi some hints?

  • 写回答

1条回答 默认 最新

  • duanbodai5166 2014-08-04 12:57
    关注

    you can upload you're file with ajax if you create a controller with php:

    <?php
        $valid_exts = array('jpeg', 'jpg', 'png', 'gif'); // Autorized extensions
        $max_size = 200 * 1024; // Max file size
        $path = 'uploads/'; // Folder where to upload
        if ($_SERVER['REQUEST_METHOD'] === 'POST') 
        {
            if( ! empty($_FILES['image']) ) 
            {
                // Get the extension of the file
                $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
                // Test the file format if it's allowed
                if (in_array($ext, $valid_exts) AND $_FILES['image']['size'] < $max_size) 
                {
                    $path = $path . uniqid(). '.' .$ext;
                    // Put the file in the folder of uploads
                    if (move_uploaded_file($_FILES['image']['tmp_name'], $path))
                        echo $path; // returning the path
                    else
                        echo 'uploads/err.gif'; // returning the error message or path or whatever
                } 
                else 
                    echo 'uploads/err.gif';
            } 
            else
                echo 'uploads/err.gif';
        } 
        else
            echo 'uploads/err.gif';
    ?>
    

    In you're view, you have to include jQuery.form library and. you create a form, and inside the form you put an image and a button, the form should have the link of you're php file as action, and call this function:

    $('#form').ajaxUpload($('#button'),$('#image_preview'));
    

    The definition of ajaxUpload function is this:

    jQuery.fn.ajaxUpload = function(Button,Preview)
    {
        var Frm = $(this); // form
        var Btn = Button; // upload button
        var Prev = Preview; // preview area 
        Btn.click(function()
        {
            // implement with ajaxForm Plugin
            Frm.ajaxForm(
            {
                beforeSend: function()
                {
                    Btn.attr('disabled', 'disabled');
                    Prev.fadeOut();
                },
                success: function(Result)
                {
                    Frm.resetForm();
                    Btn.removeAttr('disabled');
                    Prev.attr("src",Result).fadeIn();
                },
                error: function(Result)
                {
                    Btn.removeAttr('disabled');
                    Prev.attr("src",Result).fadeIn();
                }
            });
        });
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)