【问题标题】:How to upload image using Ajax in PHP?如何在 PHP 中使用 Ajax 上传图像?
【发布时间】:2012-01-28 07:03:47
【问题描述】:

我有一个表格

<form id="profile_imageform" class="image_form" enctype="multipart/form-data">

    <fieldset>
        <div class="entry">
            <label>Filename:</label>
            <input type="file" name="file" id="file" />
        </div>
    </fieldset>
    <div class="actions">
        <button type="submit">Save &amp; Close</button>( <a href="#/profile" class="cancel">Cancel</a> )
    </div>
</form>

我的 js 文件看起来像

ProfileForm.prototype.init = function(){ var self = this;

    //Initialize properties
    this.view = $( "#profile_form_view" );
    this.imageForm = $( "#profile_imageform" );
    this.fields.id = this.imageForm.find( ":input[ name = 'id' ]" );
    this.fields.image = this.imageForm.find( ":input[ name = 'file' ]" );

    //Bind the submit handler
    this.imageForm.submit(function( event ){

        //Submit the form
        self.saveImage(this.fields.id.val(), this.fields.image.val());

        //Cancel default event
        return( false );

    });

 ProfileForm.prototype.saveImage = function( id, image, onSuccess, onError ){
    var self = this;

    application.ajax({
        url: "test.php",
        data: {
            method: "saveImage",
            id: id,
            image: image
        },
        success: function( response ){
            if (response.success){
                onSuccess( response.data );
            }else{
                onError( response.errors );
            }
        }

    });
};

但是

this.fields.image.val() 

返回我需要的图像名称是 tmp_name。 是否有可能在 jQuery 中获得它的 tmp_name ?如果有怎么办?

但是这个 php 代码也返回错误

if (isset($_POST['method']))
{
    if (isset($_POST['image']))
    {
        echo $_FILES['image']['tmp_name'];
    }
}

【问题讨论】:

    标签: php jquery


    【解决方案1】:

    如果你不做任何错误检查,不要期望错误响应......

    function file_upload_error_message($error_code) {
        switch ($error_code) {
        case UPLOAD_ERR_INI_SIZE:
            return 'The uploaded file exceeds the upload_max_filesize directive in    php.ini';
        case UPLOAD_ERR_FORM_SIZE:
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
        case UPLOAD_ERR_PARTIAL:
            return 'The uploaded file was only partially uploaded';
        case UPLOAD_ERR_NO_FILE:
            return 'No file was uploaded';
        case UPLOAD_ERR_NO_TMP_DIR:
            return 'Missing a temporary folder';
        case UPLOAD_ERR_CANT_WRITE:
            return 'Failed to write file to disk';
        case UPLOAD_ERR_EXTENSION:
            return 'File upload stopped by extension';
        default:
            return 'Unknown upload error';
    }
    } 
    

    【讨论】:

    • 我想在 jQuery 中获取该图像的临时名称。我怎么能这样做?请帮助
    • 最好的方法是从你的服务器端语言(?)获得响应......然后将它重新放回你的回调,你使用什么语言?
    • 好吧,对不起,我重新读了你的问题..PHP。您的表单一开始设置错误,您的表单输入需要是 type=file 并且您之后的键(名称)是图像! === $_FILES['image'] 错误,$_FILES['image'] === name=image
    • 这一行存储 this.fields.image = this.imageForm.find( ":input[ name = 'file' ]" );图像输入字段,当我输入 this.fields.image.val() 时,它返回文件名。我的问题是,是否有任何 jquery 方法来获取图像的 tmp_name???
    • 当你处理表单时,经验法则是name=KEY value=VALUE,所以就PHP [NAME]=>VALUE而言,回答你之前的问题this.imageForm.find(" input[name=file]") 仅在您重新构建 POSTED 数组时才有效,例如 "var post = { image : this.imageForm.find("input[name=file]") } " 然后 $_POST('图像') 将 === 文件
    【解决方案2】:

    您可以使用 JQuery 文件上传插件脚本 - Uploadify

    http://www.uploadify.com/demos/

    http://www.uploadify.com/documentation/

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 2015-05-29
      • 2019-09-12
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      相关资源
      最近更新 更多