【发布时间】:2016-11-18 07:27:29
【问题描述】:
我正在尝试使用 jquery 将图像上传到 codeginter 中的文件夹。但我无法了解为什么图像没有上传并显示消息的确切问题
您没有选择要上传的文件。
我无法理解为什么没有选择在此处上传文件。她是我的php代码
public function add_new_book()
{
$image = $this->input->post('bookfile');
$img=$this->input->post('bookfile');
$config['upload_path'] = '/uploads';
$config['overwrite'] = 'TRUE';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config["max_size"] = '1400';
$config["max_width"] = '1400';
$config["max_height"] = '1400';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('bookfile'))
{
$this->data['error'] = $this->upload->display_errors();
echo json_encode(array("result"=>$this->data['error']));
exit;
}
}
我在这里写jquery代码
$( document ).ready(function() {
$("#btnsavebook").click(function(){
if($("#bookfileid").val() != ''){
if (typeof FormData !== 'undefined') {
var formData = new FormData($("#form-upload")[0]);
console.log("formdata:",formData)
$.ajax({
type: "POST",
url: "CreateBook/add_new_book",
data: formData,
mimeType:"multipart/form-data",
dataType: 'json',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
cache:false,
contentType: false,
processData: false,
success: function(result){
}
});
} }
});});
谁能告诉我如何实现这个过程??
谢谢
【问题讨论】:
-
是输入类型文件名'bookfile'吗?
-
是的@cssBlaster21895
-
查看我的解决方案,stackoverflow.com/questions/40582426/… 它肯定会帮助你,只需从我的案例中上传单张图片即可。
-
$img=$this->input->post('bookfile');这行可以影响 $_FILES,也许删除它。还要检查 echo json 编码 $_FILES 中的内容。
标签: php jquery codeigniter