Image Upload via HttpClient

JakovljevicIgorJakovljevicIgor USMember ✭✭✭
edited April 2017 in Xamarin.iOS

Hi all,

I am experiencing some issues while uploading Images via an iPad App. I have a shared PCL for the image upload logic. The upload works perfectly for WPF and Win Forms Applications. Even If I upload a 15MB sized Image, the upload works.

I am trying to use the same PCL for my iOS Project. When I upload images that are less than 1.3 MB it works, but with larger images it crashes. It is strange to me because the behavior of the shared PCL should be the same on any platform (in an ideal case).

Has anyone experienced similar problems?

Thanks,
Igor

Posts

  • prashantvcprashantvc USXamarin Team Xamurai

    Could you share what have you tried so far? iOS usually does not allow uploading large file at once.
    You may want to upload the image in chunks

  • JakovljevicIgorJakovljevicIgor USMember ✭✭✭

    @prashantvc -- Hi thanks for your help. I found the solution the to initialize the "MultipartFormDataContent" with a boundary and the set the image content header type to "application/octet-stream"

    using (var client = new HttpClient(new HttpClientHandler {Credentials = new NetworkCredential(Constants.RestApiUserName, Constants.RestApiPassword) }))
    {
    client.DefaultRequestHeaders.TransferEncodingChunked = true;
    var boundary = "---8d0f01e6b3b5dafaaadaad";
    var content = new MultipartFormDataContent(boundary);
    using (var memoryStream = new MemoryStream(file))
    {
    using (var imageContent = new StreamContent(memoryStream))
    {
    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse(fileType);
    content.Add(imageContent, Constants.FileUploadParameter, encodedFileName);
    var result = await client.PostAsync(new Uri(Constants.RESTAPI_Upload), content);
    return result;
    }
    }
    }

  • Sammy002Sammy002 AUMember

    Hi @JakovljevicIgor and @prashantvc ,

    Please help me, I am facing same issue for ios upload byte array image file to sharepoint. I use exactly same code as Igor show. But on SharePoint site, it show as empty image. I use HttpClient.SendAsync(HttpRequestMessage) because of the way Igor code requesting to sharepoint, make me Authentication error. I am beginner for mobile app dev, My code are below....

            string digestToken = await base.GetFormDigest();
            string url = attachmentURL + "/add(Filename='" + item.FileName + "')";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    
            request.Headers.Add("ContentLength", (item.fileContent.Length).ToString());
            request.Method = HttpMethod.Post;
            request.Headers.Add("binaryStringRequestBody", "true");
            request.Headers.Add("X-RequestDigest", digestToken);
    

    ---- HTTPClient has been initialized before for authentication and download some staff.

                client.DefaultRequestHeaders.TransferEncodingChunked = true;
                var boundary = "---8d0f01e6b3b5dafaaadaad";
                var content = new MultipartFormDataContent(boundary);
                using (var imageContent = new ByteArrayContent(ReadToEnd(item.fileContent)))
                {
                    imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                    content.Add(imageContent, "image", item.FileName);
                    request.Content = content;
                    var result = await client.SendAsync(request);
    
                    Debug.WriteLine(" == result ==" + result.ReasonPhrase);
                }
    

    Regard,
    Sam

Sign In or Register to comment.