Hi rvanjare97,
Refer the below example and change as per your requirement.
Database
CREATE TABLE [StudentFiles]
(
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[StudentId] [int] NOT NULL,
[FileName] [varchar](max) NULL,
[Path] [nvarchar](max) NULL
)
HTML
Login Page
<table>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
</tr>
<tr>
<td></td>
<td><asp:Button Text="Login" runat="server" OnClick="OnLogin" /></td>
</tr>
</table>
Student File details
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="ViewFile"
CommandArgument='<%# Eval("FileName") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
Code
Login Page
protected void OnLogin(object sender, EventArgs e)
{
// Validate the login with your database code.
if (txtName.Text == "Student1")
{
Session["StudentId"] = 1;
}
else
{
Session["StudentId"] = 2;
}
Response.Redirect("~/CS.aspx");
}
Student File details
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
protected void Upload(object sender, EventArgs e)
{
string studentId = Session["StudentId"].ToString();
string fileName = string.Format("{0}_{1}", studentId, 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 StudentFiles VALUES (@StudentId, @Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@StudentId", studentId);
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void ViewFile(object sender, EventArgs e)
{
string url = string.Format("View.aspx?FileName={0}", (sender as LinkButton).CommandArgument);
string script = "<script type='text/javascript'>window.open('" + url + "')</script>";
this.ClientScript.RegisterStartupScript(this.GetType(), "script", script);
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM StudentFiles WHERE StudentId = @StudentId";
cmd.Parameters.AddWithValue("@StudentId", Session["StudentId"]);
cmd.Connection = con;
con.Open();
gvFiles.DataSource = cmd.ExecuteReader();
gvFiles.DataBind();
con.Close();
}
}
}
View Page
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string filePath = Server.MapPath("~/Uploads/") + Request.QueryString["FileName"];
this.Response.ContentType = "application/pdf";
this.Response.AppendHeader("Content-Disposition;", "attachment;filename=" + Request.QueryString["FileName"]);
this.Response.WriteFile(filePath);
this.Response.End();
}
}
Screenshot