Not able to Insert Image data for two images Postgresql

Anonymous
2024-06-11T19:37:32.6+00:00
 [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,338 questions
Azure Database for PostgreSQL
Azure Startups
Azure Startups
Azure: A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.Startups: Companies that are in their initial stages of business and typically developing a business model and seeking financing.
236 questions
0 comments No comments
{count} votes

Accepted answer
  1. JasonPan - MSFT 4,791 Reputation points Microsoft Vendor
    2024-06-12T01:52:31.7766667+00:00

    Hi @Maui Learner,

    Add enctype="multipart/form-data" in your form tag like below, then the issue will be fixed.

    <form asp-action="Create" enctype="multipart/form-data">
    

    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.

    Best regards,

    Jason

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 50,506 Reputation points
    2024-06-11T19:47:03.88+00:00

    The value 'Capturetest1.png' is not valid for Imagedata.

    I'm not sure I understand what you mean by this. In your HTML you have identified the input as type file. That means the user will get browse button and the ability to select a file. The underlying value is going to be a string.

    In you model you are passing byte arrays for the 2 images. That is the actual file contents. You cannot bind the file contents to an input field marked as type file. If you want to display the filename in the HTML then in the model change the properties to strings. Then assign the filename to be shown to the properties, don't load the file itself.

    If you want to show the images in the HTML then you'll need to add an img element instead. Set the URL of the image to be the URL to the image file. If you're storing the image file on disk then use the relative path to that file otherwise you'll have to create a server endpoint to expose the image. It is that code that will be responsible for sending the byte array to the browser.

    You can see a full example of how to do this in the ASP.NET Docs. Refer to their example code on how to properly set up your HTML and model backend.

    Finally note that your code appears incorrect to me. If imagedata != null then you do work to load the image and store it in question.Imagedata. You then do the same thing for imagesol. However you have a copy/paste error in that it is assigning the results to question.Imagedata instead of question.Imagesol.