将视频上传到文件夹并将文件路径复制到数据库,然后从datagridview播放视频 [英] Upload videos to folder and copy filepath to database, then play video from datagridview

查看:83
本文介绍了将视频上传到文件夹并将文件路径复制到数据库,然后从datagridview播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在我的网站上,我成功地将图像上传到项目文件夹中的文件夹位置,并将文件路径复制到数据库中,以便可以在数据网格视图中显示和查看图像。我现在想要做的是以相同的方式上传视频,然后当它们显示在数据网格视图中时,在弹出播放器或新窗口中播放它们?我怎么会这样,因为我不知道?



这是我上传文件并显示它们的代码到目前为止



 受保护  void  UploadFile( object  sender,EventArgs e)
{
if (btnBrowse.PostedFile!= null && btnBrowse.PostedFile.ContentLength > 0
{


try
{
string fileName = Path.GetFileName(btnBrowse.PostedFile.FileName);
string folder = Server.MapPath( 〜/ BusinessProfilesContent / + Session [ BusinessName]);
Directory.CreateDirectory(文件夹);
btnBrowse.PostedFile.SaveAs(Path.Combine(folder,fileName));

// 将数据添加到数据库和文件路径
SqlConnection conn;
SqlCommand comm;
字符串 connectionString = ConfigurationManager.ConnectionStrings [ ApplicationServices]的ConnectionString。
conn = new SqlConnection(connectionString);
comm = new SqlCommand( exec UploadContent @ Description,@ DateTime,@ FilePath,@ FileName,@ BusinessName,@ Username,@ Incident_Name,conn);
comm.Parameters.Add( @ Username,System.Data.SqlDbType.VarChar ).Value = Session [ User];
comm.Parameters.Add( @ Description,System.Data.SqlDbType.VarChar ).Value = txtAddComments.Text.Trim();
comm.Parameters.Add( @ DateTime,System.Data.SqlDbType.DateTime ).Value = System.DateTime.Now;
comm.Parameters.AddWithValue( @ FilePath 〜/ BusinessProfilesContent / + Session [ BusinessName] + / + fileName);
comm.Parameters.AddWithValue( @ FileName,fileName);
comm.Parameters.Add( @ BusinessName,System.Data.SqlDbType.VarChar ).Value = Session [ BusinessName];
comm.Parameters.Add( @ Incident_Name,System.Data.SqlDbType.VarChar ).Value = DDLIncidentType.Text;

conn.Open();
comm.ExecuteNonQuery();

Response.Write( Uploaded: + fileName);


}
catch
{
Response.Write( 操作失败!!!);
}
}



}
受保护 void Page_Load( object sender,EventArgs e)
{
Label1.Text = Welcome + Session [ 用户];
Label2.Text = 业务: +会话[ BusinessName];

SqlConnection conn;
SqlCommand comm;
字符串 connectionString = ConfigurationManager.ConnectionStrings [ ApplicationServices]的ConnectionString。

conn = new SqlConnection(connectionString);
comm = new SqlCommand( SELECT *来自Business a,User_Acc c其中c.Username = @Username AND a.Business_ID = c.Business_ID,conn);
comm.Parameters.Add( @ Username,System.Data.SqlDbType.VarChar ).Value = Session [ User];
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
lblOwner.Text + = reader [ Owner_Name];
lblBusiness.Text + = reader [ Business_Name];
lblAddress.Text + = reader [ Address_Line_1] + - + reader [ Address_Line_2] + - + reader [ Address_Line_3];
lblProvince.Text + = reader [ 普罗旺斯];
lblCounty.Text + = reader [ ];
lblVATNo.Text + = reader [ Vat_No];
lblEmail.Text + = reader [ Email];
lblTelephone.Text + = reader [ Telephone];

}
SqlConnection connWrite;
connWrite = new SqlConnection(connectionString);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();

SqlCommand comm1 = new SqlCommand( exec DisplayContent @BusinessName,connWrite);
comm1.Parameters.Add( @ BusinessName,System.Data.SqlDbType.VarChar ).Value = Session [ BusinessName];
尝试
{
connWrite.Open();
sda.SelectCommand = comm1;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (例外情况)
{
Response.Write(ex.Message);
}
最后
{
connWrite.Close();
sda.Dispose();
connWrite.Dispose();
}




reader.Close();
conn.Close();

}

protected void GridView1_RowCommand(< span class =code-keyword> object
sender,GridViewCommandEventArgs e)
{
try
{
switch (e.CommandName)
{
case view
string filename = e .CommandArgument.ToString();
string path = Server.MapPath( 〜/ BusinessProfilesContent / + Session [ BusinessName] + / + filename);
byte [] bts = System.IO.File.ReadAllBytes(path);
Response.Clear();
Response.ClearHeaders();
Response.ContentType = image;
Response.WriteFile(path);
Response.Flush();
Response.End();
break ;
}
}
catch (exception ex)
{
Response.Write(ex.Message) ;

}
}





 <   asp:TableRow     ID   =  TableRow9    runat   =  server >  
< asp:TableCell ID = TableCell17 runat = 服务器 Horizo​​ntalAlign = 中心 VerticalAlign = Top > 上传新内容:< / asp:TableCell >
< asp:TableCell ID = TableCell18 runat = server Horizo​​ntalAlign = VerticalAlign = 热门 > < asp:FileUpload < span class =code-attribute> ID = btnBrowse runat = 服务器 / >
< / asp:TableCell >
< / asp:TableRow >

< asp:TableRow ID = TableRow12 runat = 服务器 >
< asp:TableCell ID = TableCell23 runat = server Horizo​​ntalAli gn = 中心 > < asp:按钮 ID = btnUpload runat = server 高度 = 22px 文字 = 上传

宽度 = 73px OnClick = UploadFile / >
< / asp:TableCell >
< / asp:TableRow >

< asp: GridView ID = GridView1 runat = server AutoGenerateColumns = false

EmptyDataText = 没有上传文件 宽度 = 834px onrowcommand = GridView1_RowCommand >
< >
< asp:BoundField DataField = Incident_ID HeaderText = 事件ID / >
< as p:BoundField DataField = Incident_Type_Name HeaderText = 事件名称 / >
< asp:BoundField DataField = < span class =code-keyword>描述 HeaderText = 说明 / < span class =code-keyword>>
< asp:BoundField DataField = Date_Time HeaderText = 日期和时间上传 / >
< asp:BoundField DataField = Business_Name HeaderText = 商家名称 / >
< asp:BoundField DataField = 普罗旺斯 HeaderText = 普罗旺斯 / >
< asp:BoundField DataField = HeaderText = / >
< asp:BoundField DataField = 用户名 HeaderText = 用户名 / >
< asp:ImageField DataImageUrlField = File_Path ControlStyle-Width = 100

ControlStyle-Height = 100 HeaderText = 预览图片 / >
< asp:TemplateField ItemStyle-Horizo​​ntalAlign = 中心 >
< < span class =code-leadattribute> ItemTemplate >
< asp:LinkBut​​ton ID = LinkBut​​ton1 runat = server CausesValidation = false CommandName = view CommandArgument =' <% #Eval( FileName)%>' > ViewFile < / asp:LinkBut​​ton >
< / ItemTemplate >
< span class =code-keyword>< / asp:TemplateField >
< /列 >
< / asp:GridView > ;





我的事件表是这样的:



Incident_ID int

Business_ID int

User_ID int

Incident_Type_ID int

描述varchar(50)

Date_Time datetime

File_Path varchar(200)

FileName varchar(200)

解决方案

< blockquote>

引用:

我甚至无法上传视频 - 当我在FileUpload控件中插入视频文件并点击上传按钮时,我得到了一个'页面无法加载'错误

请浏览一些文章,了解视频的上传方式。



1. 使用C#和VB.Net上传保存检索和播放MP4视频文件,使用来自ASP.Net中数据库的实时流媒体 [ ^ ]

2. 视频上传和查看asp.net c# [ ^ ]


Hi Everyone,

On my website I am successfully able to upload images to a folder location in my project folder and copy the file path to the database so that the images can be displayed and viewed in a data grid view. What I want to be able to do now is upload videos in the same way and then when they are displayed in the data grid view, play them in a pop up player or new window? How would I go about this as I have no idea?

Here is my code for uploading the files and displaying them so far

protected void UploadFile(object sender, EventArgs e)
        {
            if (btnBrowse.PostedFile != null && btnBrowse.PostedFile.ContentLength > 0)
            {

                
                try
                {
                    string fileName = Path.GetFileName(btnBrowse.PostedFile.FileName);
                    string folder = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"]);
                    Directory.CreateDirectory(folder);
                    btnBrowse.PostedFile.SaveAs(Path.Combine(folder, fileName));

                    //add data to the database and the file path
                    SqlConnection conn;
                    SqlCommand comm;
                    String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                    conn = new SqlConnection(connectionString);
                    comm = new SqlCommand("exec UploadContent @Description,@DateTime,@FilePath,@FileName,@BusinessName,@Username,@Incident_Name", conn);
                    comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
                    comm.Parameters.Add("@Description", System.Data.SqlDbType.VarChar).Value = txtAddComments.Text.Trim();
                    comm.Parameters.Add("@DateTime", System.Data.SqlDbType.DateTime).Value = System.DateTime.Now;
                    comm.Parameters.AddWithValue("@FilePath", "~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + fileName);
                    comm.Parameters.AddWithValue("@FileName", fileName);
                    comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                    comm.Parameters.Add("@Incident_Name", System.Data.SqlDbType.VarChar).Value = DDLIncidentType.Text;

                    conn.Open();
                    comm.ExecuteNonQuery();

                    Response.Write("Uploaded: " + fileName);


                }
                catch
                {
                    Response.Write("Operation Failed!!!");
                }
            }


           
        }
 protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "Welcome " + Session["User"];
            Label2.Text = "Business: " + Session["BusinessName"];
           
            SqlConnection conn;
            SqlCommand comm;
            String connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("SELECT * from Business a, User_Acc c Where c.Username = @Username AND a.Business_ID = c.Business_ID", conn);
            comm.Parameters.Add("@Username", System.Data.SqlDbType.VarChar).Value = Session["User"];
            conn.Open();
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                lblOwner.Text += reader["Owner_Name"];
                lblBusiness.Text += reader["Business_Name"];
                lblAddress.Text += reader["Address_Line_1"] + " - " + reader["Address_Line_2"] + " - " + reader["Address_Line_3"];
                lblProvince.Text += reader["Provence"];
                lblCounty.Text += reader["County"];
                lblVATNo.Text += reader["Vat_No"];
                lblEmail.Text += reader["Email"];
                lblTelephone.Text += reader["Telephone"];

            }
                SqlConnection connWrite;
                connWrite = new SqlConnection(connectionString);
                DataTable dt = new DataTable();
                SqlDataAdapter sda = new SqlDataAdapter();

                SqlCommand comm1 = new SqlCommand("exec DisplayContent @BusinessName", connWrite);
                    comm1.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar).Value = Session["BusinessName"];
                    try
                    {
                        connWrite.Open();
                        sda.SelectCommand = comm1;
                        sda.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        connWrite.Close();
                        sda.Dispose();
                        connWrite.Dispose();
                    }



            
            reader.Close();
            conn.Close();

        }

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "view":
                        string filename = e.CommandArgument.ToString();
                        string path = Server.MapPath("~/BusinessProfilesContent/" + Session["BusinessName"] + "/" + filename);
                        byte[] bts = System.IO.File.ReadAllBytes(path);
                        Response.Clear();
                        Response.ClearHeaders();
                        Response.ContentType = "image";
                        Response.WriteFile(path);
                        Response.Flush();
                        Response.End();
                        break;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }
        }



 <asp:TableRow ID="TableRow9" runat="server">
                <asp:TableCell ID="TableCell17" runat="server" HorizontalAlign="Center" VerticalAlign="Top">Upload New Content :</asp:TableCell>
                <asp:TableCell ID="TableCell18" runat="server" HorizontalAlign="Left" VerticalAlign="Top"><asp:FileUpload ID="btnBrowse" runat="server" />
</asp:TableCell>
            </asp:TableRow>

<asp:TableRow ID="TableRow12" runat="server">
                <asp:TableCell ID="TableCell23" runat="server" HorizontalAlign="Center"><asp:Button ID="btnUpload" runat="server" Height="22px" Text="Upload" 

            Width="73px" OnClick="UploadFile"/>
</asp:TableCell>
            </asp:TableRow>

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 

                        EmptyDataText = "No files uploaded" Width="834px" onrowcommand="GridView1_RowCommand">
    <Columns>
       <asp:BoundField DataField = "Incident_ID" HeaderText = "Incident ID" />
       <asp:BoundField DataField = "Incident_Type_Name" HeaderText = "Incident Name" />
       <asp:BoundField DataField = "Description" HeaderText = "Description" />
       <asp:BoundField DataField = "Date_Time" HeaderText = "Date and time of Upload" />
       <asp:BoundField DataField = "Business_Name" HeaderText = "Business Name" />
       <asp:BoundField DataField = "Provence" HeaderText = "Provence" />
       <asp:BoundField DataField = "County" HeaderText = "County" />
       <asp:BoundField DataField = "Username" HeaderText = "Username" />
       <asp:ImageField DataImageUrlField="File_Path" ControlStyle-Width="100"

        ControlStyle-Height = "100" HeaderText = "Preview Image"/>
         <asp:TemplateField ItemStyle-HorizontalAlign = "Center">
            <ItemTemplate>
               <asp:LinkButton ID="LinkButton1"  runat="server" CausesValidation="false" CommandName="view" CommandArgument = '<%#Eval("FileName")%>' >ViewFile</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



My Incident table is like so:

Incident_ID int
Business_ID int
User_ID int
Incident_Type_ID int
Description varchar(50)
Date_Time datetime
File_Path varchar(200)
FileName varchar(200)

解决方案

Quote:

I can't even upload video - when I insert a video file in the FileUpload control and hit the upload button I get a 'Page cannot be loaded' error

Please go through some articles to know how exactly video is uploaded.

1. Upload Save Retrieve and Play MP4 Video files with live streaming from Database in ASP.Net using C# and VB.Net[^]
2. video Upload and view in asp.net c#[^]


这篇关于将视频上传到文件夹并将文件路径复制到数据库,然后从datagridview播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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