为什么我的Ajax文件上传不起作用 - CodeIgnite,JQuery,Apache,localhost

时间:2015-08-13 16:55:26

标签: javascript php jquery ajax codeigniter

编辑:如何获取项目组合目录的文件路径,如下所示:$ config ['upload_path'] = FCPATH。 '/../portfolio/'; ?它似乎有效,但有更清洁的方法吗?

我正在尝试使用JQuery在CodeIgniter中通过Ajax上传zip文件 在这里,我只介绍相关部分,以免使帖子过于冗长。

我收到错误:上传路径似乎无效。这是来自以下行的上传处理程序php文件

$this->data['message'];
echo $this->data['message'];

并通过以下行的javascript打印

$( "#upload_error div:first-child" ).html ( data ); }

查看文件(codeIgniter views)

<form id="sign_up" action="<?php echo site_url ( 'www' ) . '/' . $this->router->fetch_class (  ) ;?>" method="post" accept-charset="utf-8" enctype="multipart/form-data">

    <input type="file" class="validate" id="user_file" name="userfile">

    <button class="btn-large" style="width: 100%;" type="submit" name="register_user" id="submit" value="Submit">Submit</button>

</form>

使用Javascript / JQuery的

var files;

$( document ).ready (   function (  ) {  
    $ ( '#user_file' ).on ( 'change', prepareUpload );
}

function prepareUpload ( event ) { files = event.target.files; }

$( '#submit' ).click ( function ( event ) {

    var data = new FormData (  );
    data.append ( 'userfile', files [ 0 ] );
    data.append ( 'ajaxUpload', 'true' );

    $.ajax ( {

        method : 'POST',
        url : scriptUrl,
        data : data,
        cache : false,
        processData: false, // Don't process the files
        contentType: false, // Set content type to false as jQuery will tell the server its a query string request
        dataType : 'html', // we will return simple true or error message from the php file
        success : function ( data, textStatus, jqXHR ) {

            if ( data === 'true' ) { 
                $ ( '#upload_success').html ( 'success' ); }
             else {
                $( "#upload_error div:first-child" ).html ( data ); }
        },
        error : function ( xhr, ajaxOptions, thrownError ) {
            alert ( 'xhr status : ' + xhr.status);
            alert ( 'thrownError : ' + thrownError);   
            alert ( 'xhr message : ' + xhr.message );
        }

    } );

}

上传处理程序文件(php)

public function do_upload (  ) {
     $this->load->library ( 'upload' );
      if (  ! $this->upload->do_upload (   )  ) {
        $this->data['message'];
        echo $this->data['message'];
    }

    else {
        echo 'true';
    }

}

最后upload.php配置文件

$config['upload_path'] = 'http://localhost/mysite/portfolio';
$config['allowed_types'] = 'zip';
$config['overwrite'] = TRUE;
$config['max_size'] = '2048KB'; // 2mb

这是目录结构

mysite
-lib
--application
---controller
---config
---model
---views
--system
-portfolio
-www
--index.php

0 个答案:

没有答案