Upload a File in Asp Net Grid From Upload
In this article I will explicate with an example, how to upload files, salvage in Folder (Directory) on Server'due south Disk and display in GridView in ASP.Net using C# and VB.Net.
The uploaded Files can be deleted and downloaded from Binder (Directory) in ASP.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView, a FileUpload command and a Button.
< asp : FileUpload ID ="FileUpload1" runat ="server" />
< asp : Button ID ="btnUpload" runat ="server" Text ="Upload" OnClick ="UploadFile" />
< hr />
< asp : GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="faux" EmptyDataText = "No files uploaded">
< Columns >
< asp : BoundField DataField ="Text" HeaderText ="File Proper noun" />
< asp : TemplateField >
< ItemTemplate >
< asp : LinkButton ID ="lnkDownload" Text = "Download" CommandArgument = ' <% # Eval("Value") %> ' runat ="server" OnClick = "DownloadFile"></ asp : LinkButton >
</ ItemTemplate >
</ asp : TemplateField >
< asp : TemplateField >
< ItemTemplate >
< asp : LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = ' <% # Eval("Value") %> ' runat = "server" OnClick = "DeleteFile" />
</ ItemTemplate >
</ asp : TemplateField >
</ Columns >
</ asp : GridView >
Namespaces
You lot will need to import the following namespace.
C#
VB.Cyberspace
Uploading the File and saving in Folder (Directory) on Server's Disk
When the File is selected in the FileUpload control and the Upload Button is clicked the following outcome handler is executed.
The uploaded File is saved in a Folder (Directory) named Uploads and the Page is redirected to itself in order to display the uploaded file in the GridView.
C#
protected void UploadFile(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub UploadFile(ByVal sender As Object, ByVal east Equally EventArgs)
Dim fileName Every bit String = Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Displaying the Files from Folder (Directory) in ASP.Net GridView
Within the Page Load event, the list of all files in the Uploads binder is fetched into a Cord Array using the GetFiles method of the Directory grade.
Later, the values of the Array are copied to a Generic Listing drove consisting objects of ListItem class and and so it is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs east)
{
if (!IsPostBack)
{
cord[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
VB.Internet
Protected Sub Page_Load(ByVal sender Every bit Object, ByVal due east Every bit EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
Dim files As List(Of ListItem) = New List(Of ListItem)
For Each filePath As String In filePaths
files.Add(New ListItem(Path.GetFileName(filePath), filePath))
Next
GridView1.DataSource = files
GridView1.DataBind()
End If
Cease Sub
Downloading the Uploaded File
When the Download Push in the GridView Row is clicked, the following result handler is executed.
The Path of the File is fetched from the CommandArgument belongings of the LinkButton raised the consequence.
Finally, using the WriteFile method of the Response course, the File is written to the Response Stream using its Path and File is downloaded.
C#
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender every bit LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.Terminate();
}
VB.Cyberspace
Protected Sub DownloadFile(ByVal sender Equally Object, ByVal e As EventArgs)
Dim filePath As Cord = CType(sender, LinkButton).CommandArgument
Response.ContentType = ContentType
Response.AppendHeader("Content-Disposition", ("attachment; filename=" + Path.GetFileName(filePath)))
Response.WriteFile(filePath)
Response.Terminate()
Cease Sub
Deleting the Uploaded File
When the Delete Button in the GridView Row is clicked, the post-obit event handler is executed.
The Path of the File is fetched from the CommandArgument property of the LinkButton raised the event.
Finally, using the Delete method of the File class, the File is deleted.
C#
protected void DeleteFile(object sender, EventArgs e)
{
cord filePath = (sender equally LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Asking.Url.AbsoluteUri);
}
VB.Net
Protected Sub DeleteFile(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath Equally String = CType(sender, LinkButton).CommandArgument
File.Delete(filePath)
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Screenshot
Demo
Downloads
Source: https://www.aspsnippets.com/Articles/Upload-files-save-in-folder-and-display-in-ASPNet-GridView-with-Download-and-Delete-option.aspx
0 Response to "Upload a File in Asp Net Grid From Upload"
Post a Comment