我正在尝试通过 ajax 上传图像,以便在单击保存按钮之前在其特定块中查看上传的图像。搜索网络我找到了以下代码并尝试了它。它工作正常,但是当我上传图像时没有上传。
html
<form id="overflow_imgupload" method="post" enctype="multipart/form-data" action="">
<input type="hidden" name="position" value="" class="position"/>
<input type="file" name="imgupload" id="imgupload" value="Upload Image" class="span12"/>
<a id="btn_imgupload" onclick="return ajaxFileUpload();">Upload</a>
<a id="closeimg" >X</a>
</form>
">
function ajaxFileUpload(){
$("#loading")
.ajaxStart(function(){
$(this).show();
})
.ajaxComplete(function(){
$(this).hide();
});
$.ajaxFileUpload({
url:'<?php echo site_url('doajaxfileupload') ?>',
secureuri:false,
fileElementId:'imgupload',
dataType: 'html',
success: function (data, status){
console.log(data);
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error: function (data, status, e){
alert(e);
}
})
return false;
}
在 doajaxfileupload.php
<?php
class Doajaxfileupload extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
//$this->load->library('upload');
}
function index(){
$error = "";
$msg = "";
$fileElementName = 'imgupload';
if(!empty($_FILES[$fileElementName]['error']))
{
switch($_FILES[$fileElementName]['error'])
{
case '1':
$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$error = 'The uploaded file was only partially uploaded';
break;
case '4':
$error = 'No file was uploaded.';
break;
case '6':
$error = 'Missing a temporary folder';
break;
case '7':
$error = 'Failed to write file to disk';
break;
case '8':
$error = 'File upload stopped by extension';
break;
case '999':
default:
$error = 'No error code avaiable';
}
}elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
{
$error = 'No file was uploaded..';
}else
{ //$msg .= $_FILES[$fileElementName]['name'];
$config['allowed_types'] = 'jpg|png|jpeg|bmp|gif';
$config['upload_path'] = './uploaded_files/';
$config['max_width'] = '720';
$config['max_height'] = '240';
$config['min_width'] = '325';
$config['min_height'] = '240';
$this->load->library('upload',$config);
if ($this->upload->do_upload($_FILES[$fileElementName]['name'])) {
$msg .= "Upload success!";
} else {
$msg .= "UPoloadfailes";
}
}
echo "{";
echo "error: '" . $error . "',\n";
echo "msg: '" . $msg . "'\n";
echo "}";
}
}
?>
每次它都会提醒UPoloadfailes。为什么?
欢迎任何帮助/建议。在此先感谢。