Loopj 库不适用于 Codeigniter 文件上传
Loopj library not working with Codeigniter File Upload
我正在尝试使用 Loopj 库从我的 Android 应用程序将图像上传到服务器。在服务器端,我使用 Codeigniter 和文件上传库。但是 Codeigniter 总是给我这个错误,即使我尝试了允许的扩展。
The file type you are attempting to upload is not allowed
这是我的图片上传代码
public void post_image() throws JSONException
{
RequestParams params = new RequestParams();
params.put("id_objek", id_objek);
params.put("id_user",id_user);
File myFile = new File(url_gambar);
try {
params.put("user_file", myFile);
} catch(FileNotFoundException e) {}
ApiRequest.post(url_gambar, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray res) {
try {
JSONObject obj = res.getJSONObject(0);
String status = obj.getString("status");
String message = obj.getString("message");
if(status=="1"){
Toast.makeText(getApplication(),"Sukses:"+message,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplication(),"Terjadi kesalahan:"+message,Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
这是我在服务器端的文件上传代码
if (empty($_FILES['user_file']['name'])) {
echo '['.json_encode(array(
'status'=>"0",
'message'=>'Gambar kosong'
)).']';
}else{
$info=null;
$config['upload_path'] = './images/gallery';
$config['allowed_types'] = 'gif|jpg|jpeg|png|jpeg|bmp';
$config['max_size'] = '5000';
$config['max_width'] = '5000';
$config['max_height'] = '2000';
$config['remove_spaces'] = 'true';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('user_file')) {
echo '['.json_encode(array(
'status'=>"0",
'message'=>$this->upload->display_errors().'-'.$_FILES['user_file']['name']
)).']';
} else {
$info = $this->upload->data();
$thumb_config['image_library'] = 'gd2';
$thumb_config['source_image'] = $info['full_path'];
$thumb_config['create_thumb'] = TRUE;
$thumb_config['maintain_ratio'] = TRUE;
$thumb_config['width'] = 600;
$thumb_config['height'] = 600;
$this->load->library('image_lib');
$this->image_lib->initialize($thumb_config);
$this->image_lib->resize();
if ($nama_gambar == null)
$nama_gambar = $info['file_name'];
echo '['.json_encode(array(
'status'=>"1",
'message'=>$nama_gambar
)).']';
}
}
如有任何帮助或建议,我们将不胜感激!
现在我在服务器端更改文件上传代码后我的代码可以工作了。我在没有 Codeigniter 文件上传库的情况下重写了我的上传代码。我一直在搜索这个问题,但找不到任何答案。这是我在服务器端的代码
...
$target_dir = "./images/gallery/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$message = "";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
/*
if (file_exists($target_file)) {
$message = "Sorry, file already exists.";
$uploadOk = 0;
}
*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$message = "Gambar terlalu besar";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$message = "Format gambar hanya bole JPG, PNG dan GIF";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$message = "Terjadi kesalahan saat upload gambar";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo '['.json_encode(array(
'status'=>"1",
'message'=>"Upload gambar sukses"
)).']';
} else {
echo '['.json_encode(array(
'status'=>"0",
'message'=>$message
)).']';
}
}
我希望这对面临同样问题的人有所帮助。
我正在尝试使用 Loopj 库从我的 Android 应用程序将图像上传到服务器。在服务器端,我使用 Codeigniter 和文件上传库。但是 Codeigniter 总是给我这个错误,即使我尝试了允许的扩展。
The file type you are attempting to upload is not allowed
这是我的图片上传代码
public void post_image() throws JSONException
{
RequestParams params = new RequestParams();
params.put("id_objek", id_objek);
params.put("id_user",id_user);
File myFile = new File(url_gambar);
try {
params.put("user_file", myFile);
} catch(FileNotFoundException e) {}
ApiRequest.post(url_gambar, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray res) {
try {
JSONObject obj = res.getJSONObject(0);
String status = obj.getString("status");
String message = obj.getString("message");
if(status=="1"){
Toast.makeText(getApplication(),"Sukses:"+message,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplication(),"Terjadi kesalahan:"+message,Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
这是我在服务器端的文件上传代码
if (empty($_FILES['user_file']['name'])) {
echo '['.json_encode(array(
'status'=>"0",
'message'=>'Gambar kosong'
)).']';
}else{
$info=null;
$config['upload_path'] = './images/gallery';
$config['allowed_types'] = 'gif|jpg|jpeg|png|jpeg|bmp';
$config['max_size'] = '5000';
$config['max_width'] = '5000';
$config['max_height'] = '2000';
$config['remove_spaces'] = 'true';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('user_file')) {
echo '['.json_encode(array(
'status'=>"0",
'message'=>$this->upload->display_errors().'-'.$_FILES['user_file']['name']
)).']';
} else {
$info = $this->upload->data();
$thumb_config['image_library'] = 'gd2';
$thumb_config['source_image'] = $info['full_path'];
$thumb_config['create_thumb'] = TRUE;
$thumb_config['maintain_ratio'] = TRUE;
$thumb_config['width'] = 600;
$thumb_config['height'] = 600;
$this->load->library('image_lib');
$this->image_lib->initialize($thumb_config);
$this->image_lib->resize();
if ($nama_gambar == null)
$nama_gambar = $info['file_name'];
echo '['.json_encode(array(
'status'=>"1",
'message'=>$nama_gambar
)).']';
}
}
如有任何帮助或建议,我们将不胜感激!
现在我在服务器端更改文件上传代码后我的代码可以工作了。我在没有 Codeigniter 文件上传库的情况下重写了我的上传代码。我一直在搜索这个问题,但找不到任何答案。这是我在服务器端的代码
...
$target_dir = "./images/gallery/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$message = "";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
/*
if (file_exists($target_file)) {
$message = "Sorry, file already exists.";
$uploadOk = 0;
}
*/
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
$message = "Gambar terlalu besar";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$message = "Format gambar hanya bole JPG, PNG dan GIF";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$message = "Terjadi kesalahan saat upload gambar";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo '['.json_encode(array(
'status'=>"1",
'message'=>"Upload gambar sukses"
)).']';
} else {
echo '['.json_encode(array(
'status'=>"0",
'message'=>$message
)).']';
}
}
我希望这对面临同样问题的人有所帮助。