如何将视频存储到sql数据库 [英] How to store a video to sql database

查看:421
本文介绍了如何将视频存储到sql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将视频存储到我的sql数据库中但没有成功。



这里是我的FileUpload



i've been trying to store a video to my sql database with no success.

here is my FileUpload

<asp:FileUpload ID="FileUpload1" runat="server" />
   <asp:Button ID="btnUploadVideo" runat="server" Text="Upload"
     OnClick="btnUploadVideo_Click" />
     <br />





这是我的btnUploadVideo_Click





Here is my btnUploadVideo_Click

protected void btnUploadVideo_Click(object sender, EventArgs e)
    {
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;

        //Set the contenttype based on File Extension
        switch (ext)
        {

            case ".avi":
                contenttype = "video/avi";
                break;
            case ".mp4":
                contenttype = "video/mp4";
                break;
            case ".wmv":
                contenttype = "video/wmv";
                break;

        }
        if (contenttype != String.Empty)
        {

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database

            string strQuery = "insert into VIDEO(Video)" +
               " values (@Video)";
            SqlCommand cmd = new SqlCommand(strQuery);
            //cmd.Parameters.Add("@Candidate_Image", SqlDbType.VarChar).Value = filename;
            //cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
            //  = contenttype;
            cmd.Parameters.Add("@Video", SqlDbType.Binary).Value = bytes;
            InsertUpdateData(cmd);
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "Video is Uploaded Successfully";
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File format not recognised." +
              " Upload Audio/avi/mp4/wmv formats";
        }
    }



,视频数据类型是varbinary(MAX)。



当我尝试将格式从.avi更改为png或jpg时,它会存储但不存储的视频格式。错误消息是连接已重置




on my Sql database, Video datatype is varbinary(MAX).

When i try to change the format from .avi to png or jpg it does store but video format it does not store. The error message is "The connection was reset"

The connection to the server was reset while the page was loading.     

  The site could be temporarily unavailable or too busy. Try again in a few moments.
  If you are unable to load any pages, check your computer's network connection.
  If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

我也尝试过其他浏览器,它一直给我同样的错误

I've also tried other browser it keep on giving me the same error

推荐答案

建议作为答案:



不要将视频文件存储在数据库中。最佳做法是将它们上载到服务器中的目录,然后将该文件的路径存储在数据库中。



很多在线数据库/网络提供商限制了你可以通过连接运行的数据量。这是为了确保所有用户的正常运行时间和可靠性,因为您的数据库没有专用服务器,它与在该计算机上托管的所有客户共享。
Suggestion as answer:

Do not store video files in a database. The best practice is to upload them to a directory in your server and then store the path to this file in the database.

A lot of online DB/web providers limit the amount of data you can run through the connection. This is to ensure uptime and reliability to all users, since your database doesn't have a dedicated server, it shares it with all the customers who are hosting on that machine.


这篇关于如何将视频存储到sql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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