How do you take an image (passed in as a Base64 encoded string) and save it to the server in Asp.Net C#?

StackOverflow https://stackoverflow.com/questions/1050417

Question

I want to create a function like this...

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult SaveImage(string file, string fileName)
    {

    }

Where the file is the Base64 encoded string created from the image, and the fileName is the name I want to save it as. How can I use this encoded string to write the image to the server?

Do I need to use BinaryWriter or TextWriter or some other one? And how do you decode the data to allow it to write to the server properly?

Was it helpful?

Solution

byte[] contents = Convert.FromBase64String(file);
System.IO.File.WriteAllBytes(Server.MapPath(fileName), contents);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top