我有一个表单,我正在接收文件输入,即图像,然后我通过ajax调用将数据发送到我的控制器,但我的控制器没有得到任何文件。它显示错误{“错误”:“
您没有选择要上传的文件。< / p>”} 我的表格
<form action="#" id="formParking" enctype="multipart-form-data">
<div class="input-group">
<div class="col-xs-12 col-sm-4 no_padding">
<label class="">Attachment</label>
</div>
<div class="col-xs-12 col-sm-8">
<div class="col-xs-12 no_space"><input type="file" id="images" name="images" multiple="multiple" /></div>
</div>
</div>
<div class="input-group">
<div class="col-xs-12 col-sm-4 no_padding"> </div>
<div class="col-xs-12 col-sm-8"> <a class="btn blue green text-center" id="btnSave" onclick="saveParking()" >Add</a> </div>
</div>
</form>
Ajax Call
function saveParking()
{
var url;
url = "link_to_controller_function";
$.ajax({
url : url,
type: "POST",
data: $('#formParking').serialize(),
dataType: "JSON",
success: function(data)
{
if(data.status) //if success close modal and reload ajax table
{
location.reload();
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
我的控制器
$config['upload_path'] = base_url().'uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 11111;
$config['max_height'] = 76118;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('images'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo json_encode($data);
}
答案 0 :(得分:1)
在您使用ajax发布表单时,在您的ajax中添加typealias CloseActionHandler = ()-> Void
class TestController: UIViewController {
var closeActionHandler: CloseActionHandler?
func close(_ handler:@escaping CloseActionHandler) {
self.closeActionHandler = handler
}
@IBAction func closeButtonTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
self.closeActionHandler?()
}
}
class ViewController: UIViewController {
func loadTestController(viewController: TestController) {
viewController.close {
//will be called when user will tap on close button
}
}
}
和processData
来处理表单
contentType
还将此属性$.ajax({
url : url,
type: "POST",
data: $('#formParking').serialize(),
dataType: "JSON",
processData: false,
contentType: false,
success: function(data)
{
if(data.status) //if success close modal and reload ajax table
{
location.reload();
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
添加到您的表单标记
enctype="multipart/form-data"
答案 1 :(得分:1)
纠正这个
<强>是enctype =&#34;多部分/格式数据&#34; 强>
答案 2 :(得分:1)
您的ajax代码似乎存在问题,请使用FormData代替序列化进行文件上传。
示例:
$("#uploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#gallery").html(data);
},
error: function(){}
});
}));
有关详细信息,请参阅:http://phppot.com/jquery/php-ajax-multiple-image-upload-using-jquery/
答案 3 :(得分:1)
ajax提交
$("#ID").submit(function(e){
e.preventDefault();
var formData = new FormData(this);
jQuery.ajax({
url : $(this).attr('action') or url,
type : $(this).attr('method') or method,
data : formData,
success:function(response){
//do whatever you want
},
cache: false,
contentType: false,
processData: false
});
});
你必须改变
$config['upload_path'] = base_url().'uploads';
到
$config['upload_path'] = './uploads/';
on base_url()。'uploads'指向网站的路径。
当然你必须在root中创建uploads目录。