Click here to Skip to main content
16,004,540 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI... I need the code in asp.net to upload &retrieve Files
(video,audio,image) into a database.. Can Anyone help me
????? I just try to save the files into a folder & store the path into database
Posted
Comments
What have you tried and where is the problem?

 
Share this answer
 
Comments
[no name] 10-Dec-13 10:51am    
i need the solution to upload images,video&audio into data directory & save the path into database for future retrieval of this files
Hai,

Please review my code it may help you..

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Upload Word Files to Database and Download files from database in asp.net
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:fileupload id="fileUpload1" runat="server" xmlns:asp="#unknown" /><br />
<asp:button id="btnUpload" runat="server" text="Upload" onclick="btnUpload_Click" xmlns:asp="#unknown" />
</div>
<div>
<asp:gridview id="gvDetails" runat="server" autogeneratecolumns="false" datakeynames="Id" xmlns:asp="#unknown">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<columns>
<asp:boundfield datafield="Id" headertext="Id" />
<asp:boundfield datafield="FileName" headertext="FileName" />
<asp:templatefield headertext="FilePath">
<itemtemplate>
<asp:linkbutton id="lnkDownload" runat="server" text="Download" onclick="lnkDownload_Click"></asp:linkbutton>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
</div>
</form>
</body>
</html></html>


After that write the following code in code behind

C#
using System;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.WebControls;



string strCon = "Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
}
}
// Bind Gridview Data
private void BindGridviewData()
{
using (SqlConnection con=new SqlConnection(strCon))
{
using (SqlCommand cmd=new SqlCommand())
{
cmd.CommandText = "select * from FileInformation";
cmd.Connection = con;
con.Open();
gvDetails.DataSource = cmd.ExecuteReader();
gvDetails.DataBind();
con.Close();
}
}
}
// Save files to Folder and files path in database
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
Stream str = fileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(str);
Byte[] size = br.ReadBytes((int) str.Length);
using (SqlConnection con=new SqlConnection(strCon))
{
using (SqlCommand cmd=new SqlCommand())
{
cmd.CommandText = "insert into FileInformation(FileName,FileType,FileData) values(@Name,@Type,@Data)";
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@Type", "application/word");
cmd.Parameters.AddWithValue("@Data", size);
cmd.Connection =con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGridviewData();
}
}
}
// This button click event is used to download files from gridview
protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int fileid = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value.ToString());
string name, type;
using (SqlConnection con=new SqlConnection(strCon))
{
using (SqlCommand cmd=new SqlCommand())
{
cmd.CommandText = "select FileName, FileType, FileData from FileInformation where Id=@Id";
cmd.Parameters.AddWithValue("@id", fileid);
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["FileType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" +dr["FileName"] + "\"");
Response.BinaryWrite((byte[])dr["FileData"]);
Response.End();
}
}
}
}
 
Share this answer
 
v2

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