JS文件 -
var doUpload = function(event){
event.preventDefault();
event.stopPropagation();
// get the file-input id
var fileId = document.getElementById('logo_upload');
// this variable makes an object which accept key/value pairs which could be sent via ajax.send
var formObj = new FormData();
// append currently selected file to the dataObject
formObj.append('file', fileId.files[0]);
// this is a variable to check in the php script (controller if the codeIgniter is used)
formObj.append('error-check', true);
formObj.append('finish-check', true);
// let's make the ajax request object
var ajaxReq = new XMLHttpRequest();
ajaxReq.onreadystatechange=function()
{
if (ajaxReq.readyState==4 && ajaxReq.status==200)
{
console.log('this is the response area');
console.log(ajaxReq.responseText);
}
}
ajaxReq.open('POST', serverUrl+"schoolController/logoImageuploadAction");
ajaxReq.setRequestHeader('Cache-Control', 'no-cache');ajaxRequest.send();
ajaxReq.send(formObj);
}
$(document).ready(function (e) {
$("#school_logo_upload").on('change', function(event){
$("#logo_image_form").submit();
event.preventDefault();
});
$("#school_logo_image_form").on('submit', function(event){
doUpload(event);
console.log('after upload form');
});
});
html表单 -
<form name="logo_photo" id="school_logo_image_form" enctype="multipart/form-data" action="" method="post">
<input type="file" id="school_logo_upload" name="file"/>
<!-- <input type="submit" name="upload" value="Upload" /> -->
</form>
codeigniter控制器功能 -
function logoImageuploadAction() {
//testing controller
$this->load->model('imageUploadManager');
$result = $this->imageUploadManager->schoolLogoUpload();
echo $result;
}
codeigniter模型函数 -
function schoolLogoUpload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$testArray = array('errror' => $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$testArray = array('successss' => $data);
}
return json_encode($testArray);
}
返回总是 - "{"errror":{"error":"<p>You did not select a file to upload.<\/p>"}}"
虽然我在模型中打印带有json编码的$ _FILES数组 -
{"file":{"name":"filename.gif","type":"image\/gif","tmp_name":"C:\\wamp\\tmp\\php7771.tmp","error":0,"size":12558}}
有人可以指出我在这里失踪了什么,任何帮助都会得到满足,谢谢!
答案 0 :(得分:0)
在$this->upload->do_upload()
函数中传递文件名。默认情况下,CI假设文件参数为userfile
。所以你需要做$this->upload->do_upload($_FILE['file']['name'])
答案 1 :(得分:0)
&lt; input type =“file”id =“school_logo_upload”name =“ file ”/&gt;
您应该通过将输入文件名作为参数传递给do_upload()函数来告诉CI。像这样:
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
$testArray = array('errror' => $error);
}
答案 2 :(得分:0)
ajaxReq.open(&#39; POST&#39;,serverUrl +&#34; schoolController / logoImageuploadAction&#34;); ajaxReq.setRequestHeader(&#39; Cache-Control&#39;,&#39; no-cache&#39;); ajaxRequest.send(); ajaxReq.send(formObj);
您即将发送第一个&#34; ajaxRequest.send();&#34;请删除并重试