I need a . Net web api for multiple image upload with other information ,data will be sent by Post Json,
Image will be in form of Base 64
I need a . Net web api for multiple image upload with other information ,data will be sent by Post Json,
Image will be in form of Base 64
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 22, 2023, 2:15 AM EDT
Define a model class to represent the data you want to send, including the Base64 encoded images. For example:
public class ImageUploadModel { public string Name { get; set; } public List Images { get; set; } // Other properties as needed }In your Web API controller, create an action method to handle the POST request with the JSON payload containing the image data. For example:
[HttpPost] public IHttpActionResult UploadImages(ImageUploadModel model) { // Access the data from the model string name = model.Name; List images = model.Images; // Process the images as needed (e.g., save them to disk, store in database, etc.) // Return a response indicating success or any other relevant information return Ok("Images uploaded successfully"); }In your client application or tool, construct the JSON payload with the necessary data, including the Base64 encoded images. Here's an example of how the JSON payload might look:
{ "Name": "John Doe", "Images": [ "Base64EncodedImage1", "Base64EncodedImage2", "Base64EncodedImage3" ] }Send a POST request to the Web API endpoint with the constructed JSON payload. The Web API will receive the data and invoke the UploadImages action method, where you can access and process the images.
In the UploadImages method, you can decode the Base64 encoded images using Convert.FromBase64String if you need to work with the raw image data. Alternatively, you can directly save the Base64 strings to disk or store them in a database, depending on your requirements.
Remember to handle any necessary error checking, validation, and exception handling based on your specific application needs.
Sandeep KumarRank #1,642 · 937 reputationPosted Jun 22, 2023, 2:19 AM EDT
I want pass parameter as JObject jsonParam
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 22, 2023, 3:39 AM EDT
To implement a .NET Web API for multiple image uploads with other information, where the images are in the form of Base64 encoded strings, you can follow these steps:
Create a new ASP.NET Web API project: Start by creating a new ASP.NET Web API project in Visual Studio. Choose the appropriate project template and configure it according to your needs.
Define a model for the data: Create a model class that represents the data you want to send along with the images. This model will have properties for the additional information and a property to store the Base64 encoded image data. For example:
public class ImageUploadModel { public string Name { get; set; } public List Images { get; set; } // Other properties as needed }WebApiConfig.csfile:config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue("text/html"));public class ImageUploadController : ApiController { [HttpPost] public IHttpActionResult UploadImages(ImageUploadModel model) { // Access the model properties and handle the image data string name = model.Name; List imageBase64Strings = model.Images; // Process the image data, such as saving to a database or file system return Ok(); } }Implement image processing logic: Inside the
UploadImagesaction method, you can implement the logic to handle the image data. Iterate over theimageBase64Stringslist, convert each Base64 string to an image file, and perform any necessary processing.Here's an example of how you can convert a Base64 string to an image file using
System.ConvertandSystem.IO:foreach (string base64String in imageBase64Strings) { byte[] imageBytes = Convert.FromBase64String(base64String); // Generate a unique filename or use the original filename string filename = Guid.NewGuid().ToString() + ".png"; string filePath = Path.Combine(Server.MapPath("~/Images"), filename); using (var imageFile = new FileStream(filePath, FileMode.Create)) { imageFile.Write(imageBytes, 0, imageBytes.Length); imageFile.Flush(); } // Perform any additional processing with the saved image file }Test the API endpoint: Use a tool like Postman or any other HTTP client to send a POST request to the API endpoint. Include the JSON data in the request body, containing the image data as Base64 strings along with the other information. Make sure the request's
Content-Typeheader is set toapplication/json.Example JSON request body:
{ "Name": "John Doe", "Images": ["base64string1", "base64string2"] }