我在js文件中写这个,以构建图片上传服务。我可以成功上传图片,但我一直收到解析错误,因此我无法继续前进。 我希望有人可以帮助我。测试代码如下:
$(function () {
$("#fileupload").wrap("<form id='myupload' action='/account/qacenter/post_images' method='post' enctype='multipart/form-data'></form>");
$("#fileupload").change(function(){
$("#myupload").ajaxSubmit({
dataType: 'json',
data: "{}",
type: "post",
timeout: 30000,
beforeSend: function() {
//doing something before send the file
},
uploadProgress: function(event, position, total, percentComplete) {
//doing something during the process
},
success: function(data) {
//doing something when post succeed
},
complete: function(xhr,msg){
//doing something when action complete
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
//doing something when error occurs.
}
});
});
});
警报结果如下:200 4 parsererror
我的网站由cakephp构建。如果您需要控制器中的代码,我可以提供。
行动代码位于:
function post_images()
{
$this->autoRender=false;
$uptypes=array(
'.JPG',
'.JPEG',
'.PNG',
'.PJPEG',
'.GIF',
'.BMP',
'.X-PNG'
);
$picname = $_FILES['image_uploader']['name'];
$picsize = $_FILES['image_uploader']['size'];
if ($picname != "") {
if ($picsize > 1024000) {
echo "image size must less than 1M";
exit;
}
$type = strtoupper(strstr($picname, '.'));
if (!in_array($type, $uptypes)){
echo "wrong image type";
exit;
}
$rand = rand(100, 999);
$pics = date("YmdHis") . $rand . $type;
//upload path
$pic_path = "C:/xampp/htdocs/youyisi/webroot/files/". $pics;
move_uploaded_file($_FILES['image_uploader']['tmp_name'], $pic_path);
}
$size = round($picsize/1024,2);
$arr = array(
'name'=>$picname,
'pic'=>$pics,
'size'=>$size
);
echo json_encode($arr);
}