我正在使用jquery在codeigniter中创建文件上传系统。但我在这方面遇到了一些问题。我正在获取文件名。但我不知道为什么它不在服务器上传文件。它没有在控制台给我任何回应。请告诉我,如果我遗失了什么。
点按钮上的我的Jquery代码
#include <stdio.h>
#include <stdlib.h>
void *func(int a)
{
void *retVal = NULL;
if (a==3)
{
retVal = malloc(sizeof(int));
if (retVal != NULL)
{
*((int *)(retVal)) = 5;
}
}
else if (a==4)
{
retVal = malloc(sizeof(int));
if (retVal != NULL)
{
*((char *)(retVal)) = 'b';
}
}
else
{
fprintf(stderr, "return value is NULL");
}
return retVal;
}
int main ()
{
int *ptr_int = func(3);
char *ptr_char = func(4);
fprintf(stdout, "int value = %d\n", *ptr_int);
fprintf(stdout, "char value = %c\n", *ptr_char);
free(ptr_int);
free(ptr_char);
return 0;
}
这是我的控制器代码
var post_data = $('#post').val();
if (!$('#post').val()) {
// console.log('hit');
// alert($('input[type=file]').val());
if (typeof FormData !== 'undefined') {
// send the formData
var formData = new FormData( $("#formname")[0] );
$.ajax({
url : 'Profile/uploadimg', // Controller URL
type : 'POST',
data : formData,
async : false,
cache : false,
contentType : false,
processData : false,
success : function(data) {
console.log(data);
}
});
} else {
message("Your Browser Don't support FormData API! Use IE 10 or Above!");
}
我的html表单
public function uploadimg()
{
$var = $_FILES ['fileUp'];
//print_r($var);
if($this->input->post('fileupload')) {
$config['upload_path'] = 'upload';
$config['file_name'] = $var;
$config['overwrite'] = 'TRUE';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config["max_size"] = '1024';
$config["max_width"] = '400';
$config["max_height"] = '400';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
$this->data['error'] = $this->upload->display_errors();
print_r( 'error');
} else {
print_r("success");
}
}
答案 0 :(得分:1)
请尝试让我知道。 你的ajax请求
$(document).on("submit",'#formname',function(event){
var form = $('#formname').get(0);
var formData = new FormData(form);
$.ajax({
type: "POST",
url: "<?php echo site_url("Profile/uploadimg"); ?>",
data: formData,
dataType: 'jSon',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
cache:false,
contentType: false,
processData: false,
success: function(result){
console.log(result);
}
});
event.preventDefault();
});
和控制器代码: -
public function uploadimg()
{
$var = $_FILES ['fileUp'];
$config['upload_path'] = 'upload';
$config['overwrite'] = 'TRUE';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config["max_size"] = '1024';
$config["max_width"] = '400';
$config["max_height"] = '400';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('fileUp'))
{
$this->data['error'] = $this->upload->display_errors();
echo json_encode(array("result"=>$this->data['error']));
exit;
}
else
{
echo json_encode(array("result"=>"Success"));
exit;
}
}
答案 1 :(得分:0)
将mimeType:"multipart/form-data"
添加到您的ajax调用中,如:
$.ajax({
url : 'Profile/uploadimg', // Controller URL
type : 'POST',
data : formData,
mimeType:"multipart/form-data",
async : false,
cache : false,
contentType : false,
processData : false,
success : function(data) {
console.log(data);
}
});
还要确保您的表单标记具有属性enctype="multipart/form-data"
。
评论该行:$config['file_name'] = $var;
。
您可以按如下方式获取上传的文件数据(在print_r("success");
上面添加):
$uploadFileData = $this->upload->data();
$uploadFileName = (isset($uploadFileData['file_name']) && trim($uploadFileData['file_name']))? $uploadFileData['file_name'] : '';