views:

328

answers:

1

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?

+2  A: 
byte[] contents = Convert.FromBase64String(file);
System.IO.File.WriteAllBytes(Server.MapPath(fileName), contents);
Mehrdad Afshari
Gives me an error "System.Web.Mvc.Controller.File(string, string, string) is a 'method', which is not valid in the given context"The method that handles this is in a controller... what do I need to do to get rid of the error?
Matt
Oops, it's a name collision issue between Controller.File method and System.IO.File class. Just name the complete thing to fix. Editing
Mehrdad Afshari
Perfect, thanks!
Matt