File upload, is this possible?

I have an application that is coming along very nicely. However, looking online one of my app requirements requires the ability to take a photo/small file (.doc,.csv,.pdf), nothing huge.

And upload it to our system. I can do this via a web api 2.0 call or something else.

How can I achieve this in an elegant way?

Thanks in advance!

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    This is the code to upload a byte[] to a web API.

    async Task SendFileToServer(byte[] image, long name)
            {
                try
                {
                    Uri webService = new Uri(URL + "imageUpload/" + name);
                    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())
                                    {
                                        content.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e) { }
            }
    

    I upload images in this example, but the method accepts a byte[] you just need to make sure your Web API can handle it.

  • CameronMurrayCameronMurray USMember ✭✭

    Thats great! How large can the file roughly be? And it is compatible with .doc, .csv, .pdf because its just a byte array correct?

    Thanks!

  • seanydaseanyda GBMember ✭✭✭✭✭

    @CameronMurray said:
    Thats great! How large can the file roughly be?

    There isn't really a limit, It's like uploading a video from your phone to Youtube, just the larger the file the longer the upload will take. It's also important to bare in mind that users have limited data on mobile devices, it might be worth limiting uploading to Wi-Fi but that depends on your application requirements.

    @CameronMurray said:
    And it is compatible with .doc, .csv, .pdf because its just a byte array correct?

    You will have a separate job of converting the .PDF, .DOC or .CSV to a byte array. If you can do that and add the file extensions to the code it will all work yes.

  • CameronMurrayCameronMurray USMember ✭✭

    Fantastic this is very helpful. I have one last question however, is there an easy way to wire up an explorer window so i can access the gallery/my docs? I'm pretty sure i have to modify the manifest files for both ios/android projects to allow this to happen but i'm still pretty new to this type of development.

  • seanydaseanyda GBMember ✭✭✭✭✭

    @CameronMurray said:
    Fantastic this is very helpful. I have one last question however, is there an easy way to wire up an explorer window so i can access the gallery/my docs? I'm pretty sure i have to modify the manifest files for both ios/android projects to allow this to happen but i'm still pretty new to this type of development.

    Sorry bud, I don't write up the API's so not able to help you there. I think it's possible to return a list of files/folders but I wouldn't know where to start :pensive:

  • VickVick Member ✭✭

    @seanyda -When I am using this code I am getting the error -

    {StatusCode: 406, ReasonPhrase: 'Not Acceptable', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Cache-Control: no-cache
    Pragma: no-cache
    Server:
    X-Powered-By:
    Strict-Transport-Security: max-age=31536000; includeSubdomains
    X-ASPNETMVC-VERSION:
    X-ASPNET-VERSION:
    Date: Tue, 10 Apr 2018 08:52:44 GMT
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Content-Length: 6
    }}

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Vick said:
    @seanyda -When I am using this code I am getting the error -

    {StatusCode: 406, ReasonPhrase: 'Not Acceptable', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Cache-Control: no-cache
    Pragma: no-cache
    Server:
    X-Powered-By:
    Strict-Transport-Security: max-age=31536000; includeSubdomains
    X-ASPNETMVC-VERSION:
    X-ASPNET-VERSION:
    Date: Tue, 10 Apr 2018 08:52:44 GMT
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Content-Length: 6
    }}

    Your web service is returning the status code 406 which is "Not Acceptable", that's not an issue with the SendFileToServer code. Your web service is expecting something else.

  • VickVick Member ✭✭

    @seanyda -- Bt It uploads successfully when i post it through ARC Client.

Sign In or Register to comment.