Server side
public class FileController : ApiController
{
public HttpResponseMessage PostFile()
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = HttpContext.Current.Server.MapPath("~/App_Data/Uploads/" + postedFile.FileName);
postedFile.SaveAs(filePath);
}
return Request.CreateResponse(HttpStatusCode.Created);
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Client side
private void sendFileCrashReport()
{
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
String url = "http://hostname/File/PostFile";
File file = new File(MainActivity.this.getFilesDir().getAbsolutePath(),
"FileText.txt");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
//reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode()== HttpsURLConnection.HTTP_CREATED)
{
android.util.Log.i(TAG,"HttpCreated");
}
else {
android.util.Log.i(TAG,"HttpFail");
}
//Do something with response...
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
I had already a file in Internal, and i want send it to server.
response.getStatusLine().getStatusCode() alway is 404, "Page not found", i tried:
http://hostname/File/PostFile
http://hostname/api/File/PostFile
Anyone know what i went wrong ? or what thing exacly in that url ? I references two link below: