How to Convert Base64 string To Video in C#

I'm trying to convert a base64 string to video In C#, and save it in the App_Data/Video/Film folder . It's not working.

Code:

public void ConvertToVideo(string data)
    {
        byte[] ret = Convert.FromBase64String(data);
        string date = DateTime.Now.ToString().Replace(@"/", @"_").Replace(@":", @"_").Replace(@" ", @"_");
        string path = HttpContext.Current.Server.MapPath("~/App_Data/Video/Film");
        FileInfo fil = new FileInfo(path+date+".mp4");
        using (Stream sw = fil.OpenWrite())
        {
            sw.Write(ret, 0, ret.Length);
            sw.Close();
        }
    }

Error: The input is not a valid Base-64 String as it contain...

Error When Convert Base64 To Video

Upvotes: 2

Views: 5132

Answers (1)

CoolBots
CoolBots

Reputation: 4889

Remove the header info at the beginning of the string, and keep just the base64 portion.

public void ConvertToVideo(string data) 
{
   string base64data = data.Replace("data:video/mp4;base64,", "");
   byte[] ret = Convert.FromBase64String(data); 

   ...code
}

Upvotes: 3

Related Questions