I do know how to upload an image by ajax in a traditional way. However, with the library upload
from CodeIgniter I can't figure it out.
我知道如何以传统方式通过ajax上传图像。但是,从CodeIgniter上传库我无法弄明白。
But first, let me just tell you that I do not use a form at all.
但首先,让我告诉你,我根本不使用表格。
if(!empty($_FILES)){
$this->load->helper('global_helper');
$config['upload_path'] = base_url() + "public/images/";
$config['file_name'] = randomName();
$config['allowed_types'] = "jpg|png|bmp";
$config['max_size'] = '500';
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
This does not work because I'm receiving the following error:
这不起作用,因为我收到以下错误:
Array
(
[error] => <p>You did not select a file to upload.</p>
)
But I actually did and it was actually uploaded to the server, because if I print out the $_FILES
I receive:
但我实际上做了,它实际上传到服务器,因为如果我打印出我收到的$ _FILES:
Array
(
[0] => Array
(
[name] => no_image.png
[type] => image/png
[tmp_name] => C:\xampp\tmp\php1715.tmp
[error] => 0
[size] => 6361
)
)
Might be the problem of the input being in an Array(of Array[0])?
可能是输入在Array(Array [0])中的问题?
The javascript code is the following:
javascript代码如下:
event.stopPropagation();
event.preventDefault();
var data = new FormData();
if(picture != undefined){
$.each(picture, function (key, value) {
data.append(key, value);
});
}
$.ajax({
url: 'ajax_add',
type: 'POST',
data: data,
cache: false,
processData: false,
contentType: false,
success: function(aux){
console.log(aux);
}
});
I do not want to use the traditional way, but if I have too..
我不想用传统方式,但如果我也有...
Thanks.
1 个解决方案
#1
0
Not sure if it can work, because the file uploading class is designed to be used with a form, but you can try this
不确定它是否可以工作,因为文件上传类旨在与表单一起使用,但您可以尝试这样做
$this->upload->do_upload(0);
If it does what I think, it will select the first item of the $_FILES
array. By default, it tries to find $_FILES['userfile']
, who doesn't exists here (which is why you have the error).
如果它符合我的想法,它将选择$ _FILES数组的第一项。默认情况下,它会尝试查找$ _FILES ['userfile'],此处不存在(这就是您遇到错误的原因)。
Give it a try, and we'll see if it works :)
尝试一下,我们会看看它是否有效:)
#1
0
Not sure if it can work, because the file uploading class is designed to be used with a form, but you can try this
不确定它是否可以工作,因为文件上传类旨在与表单一起使用,但您可以尝试这样做
$this->upload->do_upload(0);
If it does what I think, it will select the first item of the $_FILES
array. By default, it tries to find $_FILES['userfile']
, who doesn't exists here (which is why you have the error).
如果它符合我的想法,它将选择$ _FILES数组的第一项。默认情况下,它会尝试查找$ _FILES ['userfile'],此处不存在(这就是您遇到错误的原因)。
Give it a try, and we'll see if it works :)
尝试一下,我们会看看它是否有效:)