我的表单需要用户上传2个文件,文件上传&的 imgupload 即可。我试图通过ajax保存整个表单。
表格和ajax代码
<form enctype="multipart/form-data" id="data">
<input type="file" id="file-upload" class="file-upload" name="file-upload" >
<input type="file" id="imgupload" class="imgupload" name="imgupload" >
<button type="submit" id="save" class="save-icon-btn">
<img src="<?php echo base_url(); ?>assets/img/Save.png">
</button>
</form>
$("#save").click(function()
{
var form_data = new FormData($('#data')[0]);
jQuery.ajax(
{
type: "POST",
url: "<?php echo base_url(); ?>" + "comp/wfc",
data: form_data,
processData: false,
contentType: false,
success: function(res)
{
console.log(res);
alert(res);
}
});
});
控制器代码
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file-upload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$data = $this->upload->data();
echo $res = $data['file_name'];
}
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|docx|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('imgupload'))
{
$error1 = array('error' => $this->upload->display_errors());
print_r($error1);
}
else
{
$data1 = $this->upload->data();
echo $res1 = $data1['file_name'];
}
问题是在控制器代码中只有1个文件被上传。如果我将文件上传保留为第一个上传代码,那么只会上传并且imgupload将无法上传。
如果我将imgupload作为第一个上传代码,那么只会上传并且上传文件不会上传。
我无法理解为什么会这样。任何人都可以告诉这个问题的解决方案
答案 0 :(得分:1)
请尝试此更新功能。你做了一些奇怪的事情,例如初始化第一个而不是第二个。由于CI是单身,这可能会导致一些问题(并不是说这是根本原因)。
$this->load->library('upload');
echo '<pre>';
print_r($_FILES);
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
if (!$this->upload->do_upload('file-upload')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = $this->upload->data();
echo $res = $data['file_name'];
}
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|docx|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->upload->initialize($config2, true);
if (!$this->upload->do_upload('imgupload')) {
$error1 = array('error' => $this->upload->display_errors());
print_r($error1);
} else {
$data1 = $this->upload->data();
echo $res1 = $data1['file_name'];
}
如果不起作用。请告诉我们$_FILES
阵列正在打印的内容。
更新:
因为$this->load->library('upload', $config2);
而无效。当CI看到加载时,它首先检查是否加载了类,而不管传递的参数是什么,因为它已经加载,这行被忽略,并且$config
(第一次图像上传)的设置被使用。
答案 1 :(得分:0)
试试这个
JS代码
$("#save").click(function()
{
var formData = new FormData();
$f1 = $('#file-upload');
$f2 = $('#imgupload');
formData.append('file-upload', $f1.get(0).files[0]);
formData.append('imgupload', $f2.get(0).files[0]);
jQuery.ajax(
{
type: "POST",
url: "<?php echo base_url(); ?>" + "comp/wfc",
data: formData,
processData: false,
contentType: false,
success: function(res)
{
console.log(res);
alert(res);
}
});
});
仅供参考,如果您不了解
,也可以尝试以下使用多个=&#34;多个&#34;
,而不是多个文件输入尝试单个文件输入<input type="file" multiple="multiple" id="file-upload" class="file-upload" name="file-upload" >