0
我一直在嘗試從android上傳圖片文件到我的php服務器,但PHP中的$ _FILES數組總是空的。爲了測試它是否是服務器或客戶端問題,我製作了一個快速的Web表單,它提交了與android應用程序相同的數據,這使得我認爲android代碼存在問題。Android多部分圖片上傳問題
我的PHP代碼:
//check for uploaded files
var_dump($_POST);
var_dump($_FILES);
$target_path = "./images/";
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename($_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
和android部分:
public void uploadFile(){
File file = new File("/storage/sdcard0/Android/data/xxx/files/Pictures/JPEG20131005.jpg");
RequestParams params = new RequestParams();
try {
params.put("uploadedfile", file);
} catch(FileNotFoundException e) {
Log.e("testcase", "file not found");
}
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://www.example.com/fileUploader.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
});
}
我剛剛回來
array(0){
}
兩個的var_dump($ _ POST);和var_dump($ _ FILES);
這是我的表單代碼其中工程:
<form action="fileUploader.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="uploadedfile" id="uploadedfile"><br>
<input type="submit" name="submit" value="Submit">
</form>
我也試過這個example和其他幾個人,所有似乎有着相似的代碼,但我得到了相同的結果,所以我一直使用結算loopJ,純粹是因爲它是一個久經考驗的庫。
看到我的答案[這裏](http://stackoverflow.com/questions/36513174/android-multipart-image-upload-with -httpconnectionparams棄用,在新的API/36513504#36513504) –