Hi rvanjare97,
Refering the below article and the table structure i have created the example.
For displaying PDF refered the below article.
Please refer the complete example.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:Repeater runat="server" ID="rptFiles">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton runat="server" CommandArgument='<%#Eval("Name") %>'
Text='<%#Eval("Name") %>' OnClick="Preview" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<hr />
<asp:Image ID="imgFile" runat="server" Height="100" Width="100" Visible="false" />
<asp:Literal ID="ltEmbed" runat="server" Visible="false" />
Namespaces
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindFiles();
}
}
private void BindFiles()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM FilesPath", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptFiles.DataSource = dt;
rptFiles.DataBind();
}
}
}
protected void Upload(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filePath = "~/Uploads/" + fileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO FilesPath VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Preview(object sender, EventArgs e)
{
imgFile.ImageUrl = string.Empty;
ltEmbed.Text = string.Empty;
string fileName = (sender as LinkButton).CommandArgument;
string extension = Path.GetExtension(fileName);
switch (extension)
{
case ".png":
case ".jpg":
case ".jpeg":
case ".gif":
imgFile.ImageUrl = "~/Uploads/" + fileName;
break;
case ".pdf":
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"300px\" height=\"200px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/Uploads/" + fileName));
break;
default:
break;
}
imgFile.Visible = !string.IsNullOrEmpty(imgFile.ImageUrl);
ltEmbed.Visible = !string.IsNullOrEmpty(ltEmbed.Text);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindFiles()
End If
End Sub
Private Sub BindFiles()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM FilesPath", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptFiles.DataSource = dt
rptFiles.DataBind()
End Using
End Using
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim filePath As String = "~/Uploads/" & fileName
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO FilesPath VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", filePath)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Protected Sub Preview(ByVal sender As Object, ByVal e As EventArgs)
imgFile.ImageUrl = String.Empty
ltEmbed.Text = String.Empty
Dim fileName As String = (TryCast(sender, LinkButton)).CommandArgument
Dim extension As String = Path.GetExtension(fileName)
Select Case extension
Case ".png", ".jpg", ".jpeg", ".gif"
imgFile.ImageUrl = "~/Uploads/" & fileName
Case ".pdf"
Dim embed As String = "<object data=""{0}"" type=""application/pdf"" width=""300px"" height=""200px"">"
embed += "If you are unable to view file, you can download from <a href = ""{0}"">here</a>"
embed += " or download <a target = ""_blank"" href = ""http://get.adobe.com/reader/"">Adobe PDF Reader</a> to view the file."
embed += "</object>"
ltEmbed.Text = String.Format(embed, ResolveUrl("~/Uploads/" & fileName))
Case Else
End Select
imgFile.Visible = Not String.IsNullOrEmpty(imgFile.ImageUrl)
ltEmbed.Visible = Not String.IsNullOrEmpty(ltEmbed.Text)
End Sub
Screenshot