Reputation: 1824
I know this question has been asked a lot on stackoverflow and its all over the internet, but it seems that there is never a common or best solution to upload an image from multi-platform to .NET REST service.
I have found this solution from a question asked before here for the service side, my first question is, What is the best and most optimized way to upload an image from Android to that specific service specified in the link ?
My second question is, how can i add a JSON with data to accompany the image being uploaded? I have seen solutions of appending the data in header param and not as a JSON? what is the perfect way to do that ?
Upvotes: 2
Views: 3770
Reputation: 8383
I have few proposals while uploading file to web service:
Dont read the whole file content while uploading to web service. Rather use HttpClient library to upload mutiple part content. See MultipartEntity.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("Service URL");
FileBody body1 = new FileBody(new File(path), mimeType);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("part0", body1);
httppost.setEntity(reqEntity);
Use android background service when uploading file. If it is big file then it will take long time to upload. If the app went to backgournd then upload process might be internupted.
Upvotes: 1
Reputation: 1391
Heres an example of a file upload using wcf rest:
[ServiceContract]
interface IUploader
{
[WebInvoke(UriTemplate = "FileUpload?public_id={public_id}&tags={tags}",
Method = "POST",
ResponseFormat = WebMessageFormat.Json)]
UploadImageResult UploadImage(Stream fileContents, string public_id, string tags);
}
public class Uploader : IUploader
{
public UploadImageResult UploadImage(Stream fileContents, string public_id, string tags)
{
try
{
byte[] buffer = new byte[32768];
FileStream fs = File.Create(Path.Combine(rootFolderPath, @"test\test.jpg"));
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
fs.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
fs.Close();
}
catch(Exception ex)
{
...
}
}
}
To call using c#
// Create the REST URL.
string requestUrl = @"http://localhost:2949/fileupload?public_id=id&tags=tags";
//file to upload make sure it exist
string filename = @"C:\temp\ImageUploaderService\vega.jpg";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
//request.ContentType = "text/plain";
request.ContentType = "application/json; charset=utf-8";
byte[] fileToSend = File.ReadAllBytes(filename);
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
{
// Send the file as body request.
requestStream.Write(fileToSend, 0, fileToSend.Length);
requestStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
string text;
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
dynamic testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
Console.WriteLine(string.Format("response url is {0}", testObj.url));
}
Question #2, you can just append the data using a key/value pair on the url of the request like in the example.
Hope that will help.
Upvotes: 0
Reputation: 4290
Regarding your 1st question:
The "best" way to upload an image from android, pretty much depends on your situation, eg:
Basically what I'm saying is, use the most obvious method for you specific needs.
Regarding question 2:
Have a look at this question.
You can use the getBase64Image method to get the image bytes on the client side and then pop that into your json you send to the server
Upvotes: 1