1

我是使用 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(): You're trying to decode an invalid JSON String: Invalid file

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

4

1 回答 1

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);
                }
            });
        }
    }],
});

尝试在谷歌上搜索 json、php json 响应、ajax/json ......

于 2013-09-12T20:27:34.260 回答