Image upload using Multipart form in Xamarin.Forms

How are you?

I tried to upload photo to server using multipart form in xamarin pcl project.
I used many open sources, but got no luck.
Can someone please share source code which is working well ?

Thanks,
Jakob

Tagged:

Answers

  • Murilo0121Murilo0121 BRMember ✭✭

    I have the same problem here.
    Anybody got it?

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited November 2016

    @JakobDinesen @Druber

    private async Task SendFileToServer(byte[] image, string URI, string shipNum, int imageCount)
            { 
                byte[] fileContents = image;
                Uri webService = new Uri(URI + shipNum);
                HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
                requestMessage.Headers.ExpectContinue = false;
    
                MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
                ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
                byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
                Guid uniqueId = Guid.NewGuid(); 
                multiPartContent.Add(byteArrayContent, "file", "image" + uniqueId + ".jpg");
                requestMessage.Content = multiPartContent;
                HttpClient httpClient = new HttpClient(new NativeMessageHandler());
                try
                {
                    HttpResponseMessage httpRequest = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
                    var responseContent = await httpRequest.Content.ReadAsStringAsync();
                    ProgressBarWidth += Percentage;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
    
  • Sean Davies ,
    is that code working on xamarin forms? ?

  • seanydaseanyda GBMember ✭✭✭✭✭

    @sumitsisodia.9614 said:
    Sean Davies ,
    is that code working on xamarin forms? ?

    Yeah, This is some better logic for it. The above code will cause memory issues.

        async Task SendFileToServer(byte[] image, long folderName)
                {
                    try
                    {
                        Uri webService = new Uri(URL + "upload/" + folderName);
                        using (var content = new MultipartFormDataContent("----MyGreatBoundary"))
                        {
                            using (var memoryStream = new MemoryStream(image))
                            {
                                using (var stream = new StreamContent(memoryStream))
                                {
                                    content.Add(stream, "file", Guid.NewGuid().ToString() + ".jpg");
                                    using (var message = await Client.PostAsync(webService, content))
                                    {
                                        if (message.ReasonPhrase.ToLower() == "OK".ToLower())
                                        {
                                            imagesToUpload.Remove(imagesToUpload.FirstOrDefault(f => f.imageBytes == image));
                                            content.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e) { }
                }
    
  • AmeenineAmeenine MYMember

    Hi Sean Davis,

    @seanyda , Your solution looks like a possible fix for me. But What is imagesToUpload?

    Thanx in advance. :smile:

    Ameen

  • seanydaseanyda GBMember ✭✭✭✭✭
    edited October 2017

    @Ameenine said:
    Hi Sean Davis,

    @seanyda , Your solution looks like a possible fix for me. But What is imagesToUpload?

    Thanx in advance. :smile:

    Ameen

    Just a class which takes an ImageSource

    public class ImageToSet
        {
            public ImageSource imageData
            {
                get;
                set;
            }
        }
    

    You could just do List of ImageSource but I might be extending on the class in the future.

  • AmeenineAmeenine MYMember

    That's Cool, @seanyda . Do you have simple sample of WCF method that receives this data? I have been trying to send the data from PCL in Stream format. But I have been getting error "Bad Request"( no extra details) for last week.

    So I want to try sending the image as bytes. please post if you have a sample. Thanx buddy

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Ameenine said:
    That's Cool, @seanyda . Do you have simple sample of WCF method that receives this data? I have been trying to send the data from PCL in Stream format. But I have been getting error "Bad Request"( no extra details) for last week.

    So I want to try sending the image as bytes. please post if you have a sample. Thanx buddy

    That is the method which receives the byte array and it stays in the PCL so it is a cross platform solution (so will work with WPF). I don't have a web API solution for you but if your API can handle receiving a byte[] that code should work.

  • AmeenineAmeenine MYMember

    Great Buddy and thanx....Sure will try this today :)

Sign In or Register to comment.