Jonathan
Jonathan

Reputation: 1669

How to convert the base64 data to video

I am recording the user video and sending the data to the controller. The controller will receive the base64 data as a string. Then i am converting the base64 string to bytes like:

public ActionResult Content(string data)
{
    byte[] ret = System.Text.Encoding.Unicode.GetBytes(data);         
    FileInfo fil = new FileInfo("D://test.mp4");
    MemoryStream stream = new MemoryStream(ret);
    var getdata = stream.GetBuffer();
    using (Stream sw = fil.OpenWrite())
      {
         sw.Write(getdata, 0, getdata.Length);
         sw.Close();
      }
}

The video is downloading but the video is not playing the content. Can any body tell me what's the reason.

Upvotes: 1

Views: 3308

Answers (1)

Oded
Oded

Reputation: 499212

You need to recover the original byte array from the base64 string - use FromBase64String for that.

public ActionResult Content(string data)
{
    byte[] ret = Convert.FromBase64String(data);         
    FileInfo fil = new FileInfo("D://test.mp4");

    using (Stream sw = fil.OpenWrite())
    {
      sw.Write(ret , 0, ret .Length);
      sw.Close();
    }
}

What your code is doing is treating the base64 string as a unicode string, which it isn't.

Upvotes: 2

Related Questions