简体   繁体   English

Android多部分图片上传问题

[英]Android Multipart Image Upload Issues

I've been trying to upload an image file from android to my php server, but the $_FILES array in php is always empty. 我一直在尝试将图像文件从android上传到我的php服务器,但是php中的$ _FILES数组始终为空。 To test if it was a server or client issue I made a quick web-form that submitted the same data as the android app, which worked, making me think that there's a problem with the android code. 为了测试这是服务器还是客户端问题,我制作了一个快速的Web表单,该表单提交了与android应用程序相同的数据,并且可以正常工作,这使我认为android代码存在问题。

My php code is: 我的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!";
}

and the android part: 和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);
            }
        });

}

I just get back 我刚回来

array(0){
}

for both var_dump($_POST); 对于两个var_dump($ _ POST); and var_dump($_FILES); 和var_dump($ _ FILES);

This is my form code which works: 这是我的工作形式代码:

<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>

I've also tried this example and a few others which all seem to share similar code but I got the same result so I've settled with using loopJ, purely because it's a tried and tested library. 我还尝试了这个示例以及其他一些似乎都共享相似代码的示例 ,但是得到了相同的结果,因此我选择使用loopJ,这纯粹是因为它是一个经过验证的库。

Ok, so it turns out the url i was sending the data to started with a www. 好的,原来我发送数据的网址是从www开始的。 and the server was giving a 302 redirect to the same domain, without the www. 并且服务器将302重定向到了没有www的相同域。 This was in turn removing all the data for some reason. 出于某种原因,这反过来又删除了所有数据。 So after a few days of hair pulling, it was just 4 simple backspaces.... 因此,经过几天的拔头发,它只是4个简单的退格键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM