我对来自iOS背景的android相当陌生。我正在尝试做一个简单的任务,将文件(用户选择的个人资料图像)发送到我的 asp.net 页面以存储在我的服务器上。
我 99% 确定我得到了正确的文件并使用我正在运行的当前代码发送它。问题是在我的 ASP.NET 文件中,文件对象的请求永远不会获得任何数据。
这是 ASP.NET 页面
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim MyFilecollection As HttpFileCollection = Request.Files
Response.Write("The count is " & MyFilecollection.Count)
'End If
End Sub
</script>
我回到应用程序的响应总是“0”
我尝试了很多不同的发送文件的方式,我不知道在这里列出它们是否谨慎,但我主要是尝试使用 AsyncHttpClient 库,如下所示:
File file = new File(Environment.getExternalStorageDirectory()+"/mygreatapp/userPhoto.jpg");
String connString = "myserverpag.aspx";
RequestParams params = new RequestParams();
try {
params.put("picture", file,"image/jpeg");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
System.out.println(params);
client.post(connString, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
Log.w("async", "success!!!!");
}
@Override
public void onFailure(Throwable e, String response) {
System.out.println(response);
e.printStackTrace();
}
});
最后,如果我在http://httpbin.org/post上发帖,我可以看到实际上我的帖子包含“文件”对象中的数据
{
"url": "http://httpbin.org/post",
"args": {},
"data": "",
"origin": "76.101.127.54",
"files": {
"picture": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYG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jjCclix65wowxI7YxPn5SO08FSLP4eimENxCXkl3LOhV8iRlyQcdcfliirnh4ytoNg08ckUzW0ZkSQ/OrFRkH3z1orZIgyfDNrHeRXNyyDYs7xQOOjqpxux25yPwrcgtIoX3KOcYrM8JlI9At5Y0nhtRAhiWTbll27jJgZIJLHIJPQdO+ms00dsslxGm4KNwRu/4/wCNTCCihWRheOb6GOy+xmQB3wSM9BXlnxCigbwS7xwxiW1mWZpFGWdcsuOvAG/PGOlSfFHXy2otcqki7jtxGxyFA6+/auKg8TpcrLZJcNJ5oMQhmgYlgeDjZnt1zSqtx6DpW5uZM5a61C3dwshzgjaAeM/h1rD1e2mluGlgYxvuDAIR+ZFHibThaalJZ3qxT+S3J3BlIOCuD0P+IrNaKwGGDrEFBxgsuP8Avk81ikj3KVTm3Yy1tZbS+lnlMju/EjF8lsevqfrmt/RLj7TeR24/ifHHrXMR2lv5u5bmZlbOQkzj+tWvD11Dp+qySQs5CoQrOxb5iCM5P1FRNaFuXL1PoOO5hPgxoISo3OqhV53ENnjPOeD },
"headers": {
"Content-Length": "4114",
"Host": "httpbin.org",
"Content-Type": "multipart/form-data; boundary=_PbbbesptaYhtEYTBDGjHF4U6F_T1n",
"Connection": "close",
"User-Agent": "android-async-http/1.4.4 (http://loopj.com/android-async-http)",
"X-Request-Id": "b54d172b-940c-4281-9e71-e87829e888e1",
"Accept-Encoding": "gzip",
"Heroku-Request-Id": "b54d172b-940c-4281-9e71-e87829e888e1"
},
"form": {},
"json": null
}