0

I am trying to upload an image file as ByteArrayContent through my web service. I have added all the images to the shared project and set the build action as Embedded resource.

Following is my code:

var fileBytes = File.ReadAllBytes("Avatars." + selectedAvatar);
var byteContent = new ByteArrayContent(fileBytes);
content.Add(byteContent, "file", selectedAvatar);

When I try like above I am getting System.IO.FileNotFoundException: Could not find file "/Projectname.Avatars.ic_avatar01_xx.png"

Added the images directly inside a folder in the shared project like the below screenshot.

enter image description here:

I tried changing the . with a / in the file path, like below:

var fileBytes = File.ReadAllBytes("Avatars/" + selectedAvatar);
var byteContent = new ByteArrayContent(fileBytes);
content.Add(byteContent, "file", selectedAvatar);

But in that case, I am getting the System.IO.DirectoryNotFoundException: Could not find a part of the path "/Avatars/ic_avatar01_xx.png"

What is the correct way to get the path of an image file stored on a shared project?

Also tried another approach:

string avatarFileName = "Avatars/" + selectedAvatar;
var assembly = typeof(ProfilePage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
content.Add(stream, "file", avatarFileName);

But in the above case I am getting the below error:

enter image description here

Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105
  • We can only get the stream of the image if we put the image file in project directly . If you do want to implement it you need to put the image to local or sd card . Check https://stackoverflow.com/questions/54663101/how-to-check-if-the-file-exist-in-xamarin-forms . – Lucas Zhang Nov 05 '20 at 09:15
  • @LucasZhang-MSFT Is it possible to convert the stream into `System.Net.Http.HttpContent`? I need to pass the data in that format through service. – Sreejith Sree Nov 05 '20 at 09:46
  • Do you want to upload the image stream with httpclient ? – Lucas Zhang Nov 05 '20 at 10:31
  • Got solution from here: https://forums.xamarin.com/discussion/186014/xamarin-forms-get-the-path-of-an-image-file-stored-on-the-shared-project/p1?new=1 – Sreejith Sree Nov 05 '20 at 10:33
  • @LucasZhang-MSFT Yes I am trying to upload image – Sreejith Sree Nov 05 '20 at 10:34

1 Answers1

1

If you want to upload the image with Stream , you could check the following code

private async Task<string> UploadImage(Stream FileStream)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://your.url.com/");
            MultipartFormDataContent form = new MultipartFormDataContent();
            HttpContent content = new StringContent("fileToUpload");
            form.Add(content, "fileToUpload");
            content = new StreamContent(FileStream);
            content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "fileToUpload",
                FileName = "xxx.png"
            };
            form.Add(content);
            var response = await client.PostAsync("http://your.url.com/", form);
            return response.Content.ReadAsStringAsync().Result;
        }

Option 2:

You could also use the plugin FileUploaderPlugin . It support uploading multiple files at once

Uploading from a file path

CrossFileUploader.Current.UploadFileAsync("<URL HERE>", new FilePathItem("<REQUEST FIELD NAME HERE>","<FILE PATH HERE>"), new Dictionary<string, string>()
            {
               {"<HEADER KEY HERE>" , "<HEADER VALUE HERE>"}
            }
);

Option 3:

The first parameter of MultipartFormDataContent is HttpContent. To handle the stream, try using the StreamContent type which inherits from the HttpContent. Get the streamContent from the stream and add id to the MultipartFormDataContent.

string avatarFileName = "Avatars." + selectedAvatar;
var assembly = typeof(ProfilePage).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
var streamContent = new StreamContent(stream);
content.Add(streamContent, "file", avatarFileName);
Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22