Skip to content

Instantly share code, notes, and snippets.

@bkhezry
Created October 2, 2016 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkhezry/02871de6e808111ad1018b24f8dcaca0 to your computer and use it in GitHub Desktop.
Save bkhezry/02871de6e808111ad1018b24f8dcaca0 to your computer and use it in GitHub Desktop.
upload image and additional parameter in android with Fast Android Networking Library to Web Service (asmx)
[WebMethod]
public void postImage()
{
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection Files = postedContext.Request.Files;
string imageName = postedContext.Request.Form.Get("imageName");
byte[] binaryWriteArray = new byte[Files[0].InputStream.Length];
Files[0].InputStream.Read(binaryWriteArray, 0,(int)Files[0].InputStream.Length);
string strPath = Server.MapPath("~/Images/" + imageName);
FileStream objfilestream = new FileStream(strPath, FileMode.Create, FileAccess.ReadWrite);
objfilestream.Write(binaryWriteArray, 0, binaryWriteArray.Length);
objfilestream.Close();
}
private void uploadToServer(String imagePath, String imageName) {
File file = new File(imagePath);
String url = Config.BASE_UTL+"ReportSystemServices.asmx/postImage";
if (file.exists()) {
AndroidNetworking.upload(url)
.addMultipartFile("imageFile", file)
.addMultipartParameter("imageName", imageName)
.setPriority(Priority.HIGH)
.build()
.setUploadProgressListener(new UploadProgressListener() {
@Override
public void onProgress(long bytesUploaded, long totalBytes) {
// do anything with progress
}
})
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// do anything with response
}
@Override
public void onError(ANError error) {
// handle error
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment