xamarin.formsshared-project

Xamarin Forms: Get the path of an image file stored on the shared project?


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


Solution

  • 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);