如何使用fileuploadfield extjs + php上传图像

时间:2013-09-09 07:12:15

标签: php javascript json extjs file-upload

我是使用extjs的新手。而且我一直很难将图像上传到服务器。我看过很多我作为指导的例子。但我似乎可以获得成功的结果。

这是我的观点代码:

items: [{
xtype: 'fileuploadfield',
id: 'form-file',
emptyText: 'Select image',
name: 'image-upload',
buttonText: 'Browse',
buttonConfig: {
iconCls: 'upload-icon'
}
},
{
xtype: 'button',
//action: 'submit',
text: 'Upload',
handler: function(){
var form = this.up('form').getForm();
form.submit({
url: 'uploadproc.php',
waitMsg: 'Loading data...',
success: function(fp, o) {
msg('Success', 'Processed file "' + o.result.file + '" on the server');
}
});

}
}

],

这是我的uploadproc.php文件

<?php

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["image-upload"]["name"]);
$extension = end($temp);
if ((($_FILES["image-upload"]["type"] == "image/gif")
|| ($_FILES["image-upload"]["type"] == "image/jpeg")
|| ($_FILES["image-upload"]["type"] == "image/jpg")
|| ($_FILES["image-upload"]["type"] == "image/pjpeg")
|| ($_FILES["image-upload"]["type"] == "image/x-png")
|| ($_FILES["image-upload"]["type"] == "image/png"))
&& ($_FILES["image-upload"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["image-upload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["image-upload"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["image-upload"]["name"] . "<br>";
echo "Type: " . $_FILES["image-upload"]["type"] . "<br>";
echo "Size: " . ($_FILES["image-upload"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["image-upload"]["tmp_name"] . "<br>";


if (file_exists("upload/" . $_FILES["image-upload"]["name"]))
{
echo $_FILES["image-upload"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["image-upload"]["tmp_name"],
"upload/" . $_FILES["image-upload"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["image-upload"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

我一直收到这个错误:

Uncaught Ext.JSON.decode():您正在尝试解码无效的JSON字符串:文件无效

我将uploadproc.php文件保存在我的文件夹中但在js文件夹之外。我真的很陌生,如果有人可以帮助我。非常感谢你。

1 个答案:

答案 0 :(得分:2)

您的服务器应返回结构化JSON(字符串)而不是普通字符串。 JSON是javascript对象的表示,通常在客户端从JSON创建。然后Javascript使用该对象轻松地与数据交互。

我更改了uploadproc.php文件以构建JSON并将其返回。对javascript也进行了一些更改,因此显示了不同的错误/成功消息。 我增加了最小上传文件大小($ _FILES [“image-upload”] [“size”]),以字节为单位它非常小。这可能会解决“无效文件”错误。

这对我有用:

<?php
$allowedExts = array(
    "gif",
    "jpeg",
    "jpg",
    "png"
);
$temp        = explode(".", $_FILES["image-upload"]["name"]);
$extension   = end($temp);

$infoString = "";

if ((($_FILES["image-upload"]["type"] == "image/gif") || ($_FILES["image-upload"]["type"] == "image/jpeg") || ($_FILES["image-upload"]["type"] == "image/jpg") || ($_FILES["image-upload"]["type"] == "image/pjpeg") || ($_FILES["image-upload"]["type"] == "image/x-png") || ($_FILES["image-upload"]["type"] == "image/png")) && ($_FILES["image-upload"]["size"] < 9999999)
//&& ($_FILES["image-upload"]["size"] < 20000) 
    && in_array($extension, $allowedExts)) {
    if ($_FILES["image-upload"]["error"] > 0) {
        die("{'success': false, 'error': '" . $_FILES["image-upload"]["error"] . "'}");
    } else {
        $infoString .= " Upload: " . $_FILES["image-upload"]["name"] . "<br>";
        $infoString .= " Type: " . $_FILES["image-upload"]["type"] . "<br>";
        $infoString .= " Size: " . ($_FILES["image-upload"]["size"] / 1024) . " kB<br>";
        $infoString .= " Temp file: " . $_FILES["image-upload"]["tmp_name"] . "<br>";

        if (file_exists("upload/" . $_FILES["image-upload"]["name"])) {
            die("{'success': false, 'error': '" . $_FILES["image-upload"]["name"] . " already exists.'}");
        } else {
            move_uploaded_file($_FILES["image-upload"]["tmp_name"], "upload/" . $_FILES["image-upload"]["name"]);
            $infoString .= " Stored in: " . "upload/" . $_FILES["image-upload"]["name"];
        }
    }
} else {
    die("{'success': false, 'error': 'File too big or invalid!'}");
}


echo "{'success': true, 'updateInfo': '" . $infoString . "'}";

Ext.create('Ext.form.Panel', {
    renderTo: Ext.getBody(),
    items: [{
        xtype: 'fileuploadfield',
        id: 'form-file',
        emptyText: 'Select image',
        name: 'image-upload',
        buttonText: 'Browse',
        buttonConfig: {
            iconCls: 'upload-icon'
        }
    }, {
        xtype: 'button',
        //action: 'submit',
        text: 'Upload',
        handler: function () {
            var form = this.up('form').getForm();
            form.submit({
                url: 'uploadproc.php',
                waitMsg: 'Loading data...',
                success: function (form, action) {
                    Ext.Msg.alert('Successful upload!', 'Image update info: ' + action.result.updateInfo);
                },
                failure: function (form, action) {
                    Ext.Msg.alert('Failure!', 'Error info: ' + action.result.error);
                }
            });
        }
    }],
});

尝试使用google搜索json,php json响应,ajax / json ...