Hi itsme,
Check this example. Now please take its reference and correct your code.
For displaying file in new window refer below link.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<hr />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
<asp:DropDownList ID="drp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SelectedIndexChanged">
</asp:DropDownList>
Namespace
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopulateFileList();
}
}
protected void SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx?FileName=" + drp.SelectedValue);
}
private void PopulateFileList()
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Files/"));
FileInfo[] fis = di.GetFiles();
drp.DataSource = fis;
drp.DataTextField = "Name";
drp.DataValueField = "Name";
drp.DataBind();
drp.Items.Insert(0, new ListItem("Select", "0"));
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string folderPath = Server.MapPath("~/Files/");
//Check whether Directory (Folder) exists.
if (!Directory.Exists(folderPath))
{
//If Directory (Folder) does not exists. Create it.
Directory.CreateDirectory(folderPath);
}
//Save the File to the Directory (Folder).
FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
PopulateFileList();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
PopulateFileList()
End If
End Sub
Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("~/Default.aspx?FileName=" & drp.SelectedValue)
End Sub
Private Sub PopulateFileList()
Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Files/"))
Dim fis As FileInfo() = di.GetFiles()
drp.DataSource = fis
drp.DataTextField = "Name"
drp.DataValueField = "Name"
drp.DataBind()
drp.Items.Insert(0, New ListItem("Select", "0"))
End Sub
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim folderPath As String = Server.MapPath("~/Files/")
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))
PopulateFileList()
End Sub
Default Page
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["FileName"]))
{
string fileName = Request.QueryString["FileName"];
string filePath = Server.MapPath("~/Files/") + fileName;
string fileExtension = Path.GetExtension(filePath);
switch (fileExtension.ToLower())
{
case ".txt":
Response.Write(File.ReadAllText(filePath));
break;
case ".pdf":
Response.ContentType = "Application/pdf";
Response.WriteFile(filePath);
Response.End();
break;
case ".jpg":
Response.ContentType = "image/JPEG";
Response.WriteFile(filePath);
Response.End();
break;
case ".gif":
Response.ContentType = "image/GIF";
Response.WriteFile(filePath);
Response.End();
break;
default:
break;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("FileName")) Then
Dim fileName As String = Request.QueryString("FileName")
Dim filePath As String = Server.MapPath("~/Files/") & fileName
Dim fileExtension As String = Path.GetExtension(filePath)
Select Case fileExtension.ToLower()
Case ".txt"
Response.Write(File.ReadAllText(filePath))
Case ".pdf"
Response.ContentType = "Application/pdf"
Response.WriteFile(filePath)
Response.End()
Case ".jpg"
Response.ContentType = "image/JPEG"
Response.WriteFile(filePath)
Response.End()
Case ".gif"
Response.ContentType = "image/GIF"
Response.WriteFile(filePath)
Response.End()
Case Else
End Select
End If
End Sub
Screenshot