Base64和视频文件 [英] Base64 and Video File

查看:126
本文介绍了Base64和视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用webservice,它将Base64作为输入并将此base64string转换为字节数组,然后最终将mp4(例如)保存到磁盘。



现在我正在尝试播放这个mp4文件但它无法播放,因为我的原始文件正常工作。

< pre lang =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。写(imageBytes, 0 ,imageBytes.Length);
ms.WriteTo(fs);
retResult = 成功;
}

}

在此先感谢你的时间和答案

解决方案

我试过你的代码,它工作正常 - 一旦我删除你不使用的retResult变量(我运行将警告视为错误,所以它不会用它编译)

我怀疑你需要查看传递给方法的base64数据 - 如果你的文件被破坏,它可能是不正确的。



BTW:你没有需要弄乱流(但如果你这样做,你应该至少关闭并处理它们)。试试这个版本:

  public   void  UploadVideoFileNew( string  base64String, string  FileName)
{
byte [] imageBytes = Convert.FromBase64String(base64String);

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

<跨度类= 代码关键字>如果
{
File.WriteAllBytes(FullFilePath,imageBytes)(File.Exists(FullFilePath)!);
}
}

这是一回事,但更简单。


大家好,



我找到了原因,为什么它不起作用,



搜索后,我使用了以下代码写我用来传递我的方法的Base64String,(以前我用Base64EncoderDecoder软件来获取base64字符串),所以我只是传递了我从下面的代码得到的输入,并且它完美地工作..



System.IO.StreamWriter文件=新System.IO.StreamWriter(System.Web.Hosting.HostingEnvironment.MapPath( 〜/ TransientStorage /)+ Base64.txt);

file.WriteLine(base64str);

file.Close();



再次感谢所有拥有者给他们时间看看这个问题。


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.

[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

解决方案

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:

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.


这篇关于Base64和视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆