I'm making a project on Windows Phone where user can take a photo, save it on phone and next upload on my server. So there are two projects, WP8.1 and ASP.NET WEB API.
Currently I don't know how to upload photo to my server(from phone), I even don't know how to catch it in API. What's the best way to make it?
Here is my method to show on the screen(phone), picture which was taken by user.
private async void LoadCapturedphoto(string filename)
{
//load saved image
StorageFolder pictureLibrary = KnownFolders.SavedPictures;
StorageFile savedPicture = await pictureLibrary.GetFileAsync(filename);
ImageProperties imgProp = await savedPicture.Properties.GetImagePropertiesAsync();
var savedPictureStream = await savedPicture.OpenAsync(FileAccessMode.Read);
//set image properties and show the taken photo
bitmap = new WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
await bitmap.SetSourceAsync(savedPictureStream);
takenImage.Source = bitmap;
takenImage.Visibility = Visibility.Visible;
}
I think that I should convert WriteableBitmap to Byte[], send these Byte[] by API to server and on server convert Byte[] to JPG/PNG/etc.
private byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
{
WriteableBitmap bmp = bitmap;
using (Stream stream = bmp.PixelBuffer.AsStream())
{
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
Any ideas? Do you think it's good idea to make it like that? WriteableBitmap -> Byte[] and next on server Byte[] -> JPG/PNG etc. Is it even possible?
If it can be done much more easier please write some samples.
it's my method for calling api methods
public string apiCommand(string api, string json)
{
using (var httpClient = new HttpClient())
{
HttpContent content = new StringContent(json, Encoding.UTF8);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = httpClient.PostAsync(Variables.apiURL + api, content).Result;
response.EnsureSuccessStatusCode();
Task<string> responseBody = response.Content.ReadAsStringAsync();
//var msg = new MessageDialog(responseBody.Result.ToString());
if (response.StatusCode.ToString() != "OK")
{
return "ERROR: " + response.StatusCode.ToString();
}
else
{
return "SUCCES: " + responseBody.Result.ToString();
}
}
}
I handle image and word files in my project by this way, hope it helps.
1) Server side, I have a generic api method to process Json requests.
Client side, 1) call through .net code, I use system.Net.webclient,
2) call it through JavaScript, usually I will create a method in website controller and forward the json request to the method defined as above. if your javascript code and api code share same json objects, it is quite easy.
2.1) web site controller code,
2.2) JavaScript ajax call sample code:
You need to use multipart/form-data request to the server. You can send the json to the web api using payload field (another alternative is send every field separately, but you need reflection to do that.)
This maybe can help you: