MAUI: Issue with uploading project's local image to database via post API as ByteArrayContent - System.ArgumentNullException

Sreejith Sreenivasan 851 Reputation points
2024-07-04T11:31:00.18+00:00

I have added a few avatar images on the project folder named Avatars and set its Build Action to Embedded Resource.

I added it for profile image update for the user. I need to upload the selected image to our database using a POST API. Below is the code I am using:

try
{
    if (isAvatar)
    {
        string avatarFileName = "MyProjectName.Avatars." + selectedAvatar;
        var assembly = typeof(RESTServices).GetTypeInfo().Assembly;
        var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
        var streamContent = new StreamContent(stream);
        content.Add(streamContent, "file", avatarFileName);
    }
}
catch (Exception exc)
{
    System.Diagnostics.Debug.WriteLine("AvatarException:>" + exc);
}

But I am getting the below exception:

AvatarException:>System.ArgumentNullException: Value cannot be null. (Parameter 'content')
   at System.ArgumentNullException.Throw(String paramName)
   at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
   at System.Net.Http.StreamContent..ctor(Stream content)
   at ProjectName.RESTServices.SaveProfile(String jsonData, String applicationId, String siteId, FileResult photo, ByteArrayContent cameraByteContents, String cameraPicPath, String selectedAvatar, Boolean isGallery, Boolean isCamera, Boolean isAvatar) in E:\My Projects\MAUI
eedhelp-app-maui\ProjectName\RESTServices.cs:line 1004

The exception is on the below line:

var streamContent = new StreamContent(stream);

The image folder is on the project's root folder. Check the below screenshot:

User's image

Is there any mistake on the path of the selected avatar image and due to that this null issue is showing?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,223 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,171 Reputation points Microsoft Vendor
    2024-07-05T02:45:06.85+00:00

    Hello,

    Firstly, please make sure this var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");, stream is not null.

    Is stream is not null, please copy stream to MemoryStream, then set the ms.Position = 0;

     string avatarFileName = "MyProjectName.Avatars." + selectedAvatar;
            var assembly = typeof(RESTServices).GetTypeInfo().Assembly;
            var stream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{avatarFileName}");
    
     MemoryStream ms = new MemoryStream();
         stream.CopyTo(ms);
         ms.Position = 0;    
         StreamContent streamContent = new StreamContent(ms);
    
         //you can detect the streamContent is not null by the length
         var length= streamContent.Headers.ContentLength;
    

    If it is null, please set the image's build action to the MauiAsset in the Avatars folder, then read it by using var stream = await FileSystem.OpenAppPackageFileAsync("Avatars\\"+ selectedAvatar);

    Here is my tested code, you can refer to it.

    
         using var stream = await FileSystem.OpenAppPackageFileAsync("Avatars\\"+ selectedAvatar);
         MemoryStream ms = new MemoryStream();
         stream.CopyTo(ms);
         ms.Position = 0;    
         StreamContent streamContent = new StreamContent(ms);
    
         //you can detect the streamContent is not null by the length
         var length= streamContent.Headers.ContentLength;
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful