1

I would like to ask how to upload image from Windows phone 8.1. My solution is with Base64 decode, but I think, that this is not the best way...

In my Windows Phone 8.1 app I select the image and after select I decode image to base64. My idea was that I send this base64 string to my WebServise as parameter like this

 www.myservice.com/ImageUpload.aspx?img=myBase64String

and decode it on server back to the image file and upload to server.

BUT this base64 string is too long so webservice returning error "The request URL is too long."

So is another way to upload image file to web from Windows Phone 8.1

And which is better. Save image to database as base64 string or as BLOB. Thanks

EDIT:

Request in Windows phone :

public async Task<string> ImageToBase64(StorageFile MyImageFile)
        {
            Stream ms = await MyImageFile.OpenStreamForReadAsync();
            byte[] imageBytes = new byte[(int)ms.Length];
            ms.Read(imageBytes, 0, (int)ms.Length);

            string base64Image = Convert.ToBase64String(imageBytes);
            string url = "http://mobil.aspone.com/ImageUpload.aspx?Id=" + base64Image;
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(new Uri(url));
            var result = await response.Content.ReadAsStringAsync();
            string jsonString = result.ToString();
            return jsonString;
    }

ASP.NET service

 protected void Page_Load(object sender, EventArgs e)
        {
            string Id = Request.QueryString["Id"];
            //byte[] bytes = Convert.FromBase64String(Id);

            Image1.ImageUrl = "data:image/png;base64," + Id;

If(Id!=null)
{
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
             Response.Write("Uploaded");
            Response.End();
    }
else
{
Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
             Response.Write("Failed");
            Response.End();
    }
Matey
  • 197
  • 3
  • 5
  • 16
  • If it's too long for the URL, why not put it in the body of your request? – mason Nov 30 '15 at 18:19
  • In the body ? How should I do it ? Could you explain it or post example ? I never do request with body – Matey Nov 30 '15 at 19:35
  • You haven't shown how you're doing the request in the first place. Update your code to show that and then it will be easier to show you how to put the data in the body. – mason Nov 30 '15 at 19:41
  • Post has been updated – Matey Nov 30 '15 at 22:20
  • So you're using the HttpClient library. That has a `PostAsync` method that allows you to add an object as part of the request body. Look at [this tutorial](http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client) and [this question](http://stackoverflow.com/questions/15176538/net-httpclient-how-to-post-string-value) which should give you an idea of how to use it. By the way, images are not base 64 strings, so it makes more sense to me to transport and store them as byte arrays. That will reduce the amount of needless conversion. – mason Nov 30 '15 at 22:25
  • I had not idea that i can send byte Array. Thanks alot for ideas and advices :) – Matey Nov 30 '15 at 22:45
  • Base64 string might be easier for transport now that I think about it, as it may be difficult to get HttpClient to work with a byte array. – mason Nov 30 '15 at 22:48

1 Answers1

0

You can use HttpMultipartContent class for uploading from windows phone and get it on server side.

HttpMultipartFormDataContent form = new HttpMultipartFormDataContent();
                    var stream = await PickedFile.OpenAsync(FileAccessMode.Read);
                    form.Add(new HttpStreamContent(stream), "image");
                    var Response = await new HttpClient().PostAsync(new Uri(StringUtility.UploadImageUrl), form);

PickedFile is StorageFile here. This code is quite simple to use and I find it easier than converting to byte array myself. You need to get this file from server side. Hope this helps.

Rohit
  • 552
  • 3
  • 15
  • Thanks, but I'm getiing this error: {System.ArgumentException: Value does not fall within the expected range. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task).... Application crashes on this line: var Response = await new HttpClient().PostAsync(new Uri(StringUtility.UploadImageUrl), form); And i have no idea why... – Matey Dec 01 '15 at 20:02
  • 1
    It might be occurring while setting the content length of data being passed. [https://monkeyweekend.wordpress.com/2014/10/23/how-to-send-text-json-or-files-using-httpclient-postasync/] Follow these steps to upload and get file using Request.Files – Rohit Dec 03 '15 at 07:30
  • If the previous link does not work for you, here is again: http://monkeyweekend.com/2014-10-23/how-to-send-text-json-or-files-using-httpclient-postasync/ – kiewic Dec 03 '15 at 16:49
  • The `System.ArgumentException` at `PostAsync` could be maybe if your file is empty (zero-length). – kiewic Dec 03 '15 at 16:54
  • Is it Silverlight or Runtime? – iamatsundere181 Apr 12 '16 at 01:55
  • It's Runtime code i.e. WP8.1 WinRT code. Use Windows.Web.Http namespace for it. – Rohit Apr 12 '16 at 04:28