在cakephp上使用ajax上传文件

时间:2014-09-25 07:50:42

标签: jquery ajax cakephp

我正在尝试构建上传服务来上传图片。 我的cakephp控制器中的代码如下:

    $this->autoRender=false;
    $uptypes=array(
        '.JPG',
        '.JPEG',
        '.PNG',
        '.PJPEG',
        '.GIF',
        '.BMP',
        '.X-PNG'
    );

    $action='';
    if(isset($_GET['act']))
        $action = $_GET['act'];
    if($action=='delimg'){
        $filename = $_POST['imagename'];
        if(!empty($filename)){
            unlink('C:/xampp/htdocs/youyisi/webroot/files/'.$filename);
            echo '1';
        }else{
            echo 'failed to delete.';
        }
    }else{
        $picname = $_FILES['image_uploader']['name'];
        $picsize = $_FILES['image_uploader']['size'];
        if ($picname != "") {
            if ($picsize > 1024000) {
                echo 'the size of your image should less than 1M';
                exit;
            }
            $type = strtoupper(strstr($picname, '.'));
            if (!in_array($type, $uptypes)){
                echo 'wrong file type!';
                exit;
            }
            $rand = rand(100, 999);
            $pics = date("YmdHis") . $rand . $type;
            //update location path
            $pic_path = "C:/xampp/htdocs/youyisi/webroot/files/". $pics;
            move_uploaded_file($_FILES['image_uploader']['tmp_name'], $pic_path);
        }

并且javascript代码落后:

$(function () {
    var showimg = $('#image_container');
    var files = $('#message_board');
    var filename=$('.filename');
    $("#fileupload").wrap("<form id='myupload' action='/account/qacenter/post_images'       method='post' enctype='multipart/form-data'></form>");
    $("#fileupload").change(function(){
        $("#myupload").ajaxSubmit({
            dataType:  'json',
            beforeSend: function() {
                showimg.empty();

            },
            uploadProgress: function(event, position, total, percentComplete) {

            },
            success: function(data) {       
                alert('succeed');
            },
            error:function(xhr){
                 files.html(xhr.responseText);
            }
        });
    });
 });

然后我在我看来输入了这个:

  <input id="fileupload" type="file" name="image_uploader">

我发生了如下所示的错误

                                   Debug timer info
    Message                     Start Time (ms)     End Time (ms)   Duration (ms)
Core Processing (Derived)                 0                  239           239

Component initialization and startup     239                 319           80

Controller action                        319                 323           0

                                   Debug timer info
        Message                   Start Time (ms)    End Time (ms)  Duration (ms)
Core Processing (Derived)                    0          -1411631232712  -1411631232712
Component initialization and startup         239             319            80
Controller action                            319             323            0

我花了好几个小时来克服它,但我失败了。我希望有人可以帮助我。我是cakephp和jquery的新学习者。非常感谢你。

0 个答案:

没有答案