In this article, we will learn how to Save a base64 string as an image into a folder on the server using C# and Web API.I’m working on Mobile App and I got a requirement to upload images by Andriod App So that I decided to take the image as Base64 from the API and Save it to the server folder. After completing it my task, I decided to share code.
Now Let’s Start
- Create an Empty WebApi
- Create an API controller.
- Create a folder where we need to save the Images.
Now Copy Paste The Below
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
namespace DemoWebApi.Controllers
{
public class ImageModel
{
public string base64Image { get; set; }
public string imagename { get; set; }
}
public class FileUploadController : ApiController
{
// POST: api/FileUpload
public JsonResult<object> Post([FromBody]ImageModel Image)
{
try
{
string ImgName = Image.imagename;
if (!string.IsNullOrEmpty(Image.base64Image))
{
//Image image = Base64ToImage(Image.base64Image);
String path = HttpContext.Current.Server.MapPath("~/Filestorage"); //Path
//Check if directory exist
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
//If directory doesn't exist then Create it
}
string imageName = ImgName + ".jpg";
//set the image path
string imgPath = Path.Combine(path, imageName);
if (Image.base64Image.Contains("data:image"))
{
//Need To remove some header information at the beginning if image data contains
//ImageDataUrl = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD....";
//Otherwise, this will give an error.
//Remove everything in front of the DataUrl and including the first comma.
//ImageDataUrl = "9j/4AAQSkZJRgABAQAAAQABAAD...
Image.base64Image = Image.base64Image.Substring(Image.base64Image.LastIndexOf(',') + 1);
// removing extra header information
}
byte[] imageBytes = Convert.FromBase64String(Image.base64Image);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
image.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);
return Json((object)new
{
status = "Success",
});
}
return Json((object)new
{
status = "Content not found",
});
}
catch(Exception ex)
{
return Json((object)new
{
Status = false,
Message = "Something went wrong with wrong.Please try after some time",
});
}
}
}
}
Now Let’s do testing above code is working or not. Open Postman and Hit the API.
Let’s Check server folder,you will able to see your image
The post C# -Saving a base64 string as an image into a folder on server in Web Api appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- Simple Way Find and replace text in Word document using C#
- Implementing “Remember Me” Feature in ASP.NET MVC
- [Solved]-Cookie loses value when page is changed in MVC
- How to post File Upload and other form fields to one action Asp .Net MVC C#
- How To Post File and Data to API using HttpClient C#
- Create ASP.NET Core Web API Without Entity Framework
- .NET Core Web API Using Code First Entity Framework Approach
- Create Asp.Net Core Web Api Using Entity Framework Database First
- Registration form with image upload in MVC using jquery Ajax
- How to make an Inline editable table in MVC using jquery?
- CRUD operation using partial view in MVC with modal popup
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- Crud Operations in MVC Without Entity Framework
- Create Login,Signout and Registration in Asp .Net Mvc Using Entity
- Export Gridview to Excel and Csv in Asp .Net With Formatting Using c#
- How to Display Binary Image in Gridview from Database in Asp .Net c#
- [Solved]-How to Upload pdf file using jquery MVC?
- [Solved]-Uploading both data and files in FormData using Ajax MVC
- C# -Saving a base64 string as an image into a folder on server in Web Api
- [Solved]-Download pdf file from link and save in local file folder in Asp .Net
- [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET
- [Solved]-LEFT OUTER JOIN in LINQ With Where clause in C#
- INNER JOIN,RIGHT JOIN,LEFT JOIN USING LINQ IN Multiple Table C#
- [Solved]-Convert Base64 To Image And Save in folder and Display it- C#
- [Solved]-How to Overlay Two Images in .NET-C#
- How to Create Multilingual Website in Asp .Net
- C# – How to add double quotes to a string that is inside a variable
- Update an Image with Upload Button For Each Row in Gridview-Asp .Net
- How to Bind Data in DataList Control in Asp.Net – C#
- Upload and Display an Image in Gridview From Image Path in Database -C#