Click here to Skip to main content
15,030,240 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using webservice, which is taking Base64 as input and Converting this base64string to byte array and then finally saving mp4(for e.g) to disk.

Now am trying to play this mp4 file but it is not able to play, where as my original file is working.
C#
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void UploadVideoFileNew(string base64String, string FileName)
    {
        string retResult = "";
        byte[] imageBytes = Convert.FromBase64String(base64String);

                MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
                string FilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") + "\\";
                string FullFilePath = FilePath + FileName;

                FileInfo imageFile = new FileInfo(FullFilePath);
                bool fileExists = imageFile.Exists;

                FileStream fs = new FileStream(FullFilePath, FileMode.Create);
                if (fileExists == false)
                {
                    ms.Write(imageBytes, 0, imageBytes.Length);
                    ms.WriteTo(fs);
                    retResult = "successful";
                }

}
Thanks in Advance for your Time and Answers
Posted
Updated 14-Jul-13 20:57pm
v2

I tried your code, and it works ok - once I'd deleted the retResult variable you don't use (I run with "Treat Warnings as Errors" so it wouldn't compile with it in)
I would suspect that you need to look at the base64 data you are passing to the method - it is probably not correct if your file is being corrupted.

BTW: You don't need to mess with streams (but if you do you should at least Close and Dispose them). Try this version:
C#
public void UploadVideoFileNew(string base64String, string FileName)
    {
    byte[] imageBytes = Convert.FromBase64String(base64String);

    string FilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") + "\\";
    string FullFilePath = FilePath + FileName;

    if (!File.Exists(FullFilePath))
        {
        File.WriteAllBytes(FullFilePath, imageBytes);
        }
    }
Which does the same thing, but a little more simply.
   
Hi All,

I have found the reason, why it was not working,

After Searching, I have used the following code which is used to write the Base64String, which i used to pass my method, (Previously I have used Base64EncoderDecoder Software to get base64 string), So I just passed the Input which I got from below code, and It Work's Perfectly..

System.IO.StreamWriter file = new System.IO.StreamWriter(System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") + "Base64.txt");
file.WriteLine(base64str);
file.Close();

Thanks Again to All who has given their time to see into this issue.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900