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));
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)