How to upload and download files using asp net and c# Part 139

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Text Articles

All ASP .NET Slides

ASP.NET Playlist

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video, we will discuss
1. Uploading files
2. Displaying the list of files that are already uploaded
3. Downloading files

When the files are uploaded, they should be uploaded to a folder on the web server. In our case, we will be uploading to "Data" folder.

[div style="font-family:Arial"]
[asp:FileUpload ID="FileUpload1" runat="server" /]
[asp:Button ID="Button1" runat="server" Text="Upload"
OnClick="Button1_Click" /]
[br /]
[br /]
[asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand" BackColor="White"
BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4"]
[Columns]
[asp:TemplateField HeaderText="File" ShowHeader="False"]
[ItemTemplate]
[asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False"
CommandArgument='[%# Eval("File") %]'
CommandName="Download" Text='[%# Eval("File") %]']
[/asp:LinkButton]
[/ItemTemplate]
[/asp:TemplateField]
[asp:BoundField DataField="Size" HeaderText="Size in Bytes" /]
[asp:BoundField DataField="Type" HeaderText="File Type" /]
[/Columns]
[FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /]
[HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="#FFFFCC" /]
[PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
HorizontalAlign="Center" /]
[RowStyle BackColor="White" ForeColor="#330099" /]
[SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" /]
[SortedAscendingCellStyle BackColor="#FEFCEB" /]
[SortedAscendingHeaderStyle BackColor="#AF0101" /]
[SortedDescendingCellStyle BackColor="#F6F0C0" /]
[SortedDescendingHeaderStyle BackColor="#7E0000" /]
[/asp:GridView]
[/div]

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
FileUpload1.PostedFile
.SaveAs(Server.MapPath("~/Data/") + fileName);
}

DataTable dt = new DataTable();
dt.Columns.Add("File");
dt.Columns.Add("Size");
dt.Columns.Add("Type");

foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Data")))
{
FileInfo fi = new FileInfo(strfile);
dt.Rows.Add(fi.Name, fi.Length.ToString(),
GetFileTypeByExtension(fi.Extension));
}

GridView1.DataSource = dt;
GridView1.DataBind();
}

private string GetFileTypeByExtension(string fileExtension)
{
switch (fileExtension.ToLower())
{
case ".docx":
case ".doc":
return "Microsoft Word Document";
case ".xlsx":
case ".xls":
return "Microsoft Excel Document";
case ".txt":
return "Text Document";
case ".jpg":
case ".png":
return "Image";
default:
return "Unknown";
}
}

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename="
+ e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Data/")
+ e.CommandArgument);
Response.End();
}
}

Please make sure to include the following using declarations in the code behind file.
using System.IO;
using System.Data;

Make sure to replace [ with LESSTHAN and ] with GREATERTHAN symbol.
Рекомендации по теме
Комментарии
Автор

Dear Venkat, I just wanted to thank you for creating and sharing such a thorough, concise and truly excellent body of work. Your ability to convey complex programming concepts in such an understandable and accessible manner is exemplary. The breadth of content you have on offer is astounding and the fact that you do all this in such perfect English (which I assume (rightly or wrongly) is not your first language) and for free is quite frankly amazing. I'm so grateful of discovering your channel and cannot thank you enough for your knowledge and generosity.

awguitars
Автор

Saved my life...AGAIN

I JUST LOVE YOU

Liev
Автор

very use full... following the steps .. saves my timee.. Thank youu ..

durgamuthusamy
Автор

Thanks alot, the download code of just 5xlines saved my day

rasheedmalik
Автор

Thank You sir for another great video :)

vaibhavtrikolikar
Автор

just outstanding work !!
mindblowing venkat
i am following all your dotnet videos and it is helping me
thanku very much from all of us !!

virajparikh
Автор

Great.... very helpful and easy to understand video thank you so much sir

aishakhan
Автор

Thank you very sir for uploading this video. This is such a great help to other programmers. The video is very easy to understand and very informative. More POWER to you!! Salute! yaHOOoo!

burn
Автор

Thanks a lot...its helped a lot and 100% working..

sachinthorat
Автор

Hooo, thanks in millions, am sure people like me that you are blessing with your knowledge will always remember you, l no many people like me don't even known how best to thank you, But an sure God will say thanks to you.Alos people are saying storing file in database is better than saving in server so can you pls do that as well and dont forget the mater pages tutorial you promise.It is well with u and your family Thanks you sir.

oyilatechnology
Автор

Excellent video. easy to understand even for a newbie like me. The way you explain as you go is excellent!!.
Thank you for uploading this..

jadehawk
Автор

thank u sir i just copy your grid view code and past. that work fine.. (y)
you are best sir

rukhsarahmad
Автор

Thank you very much sir excelent tutorial

yudha
Автор

Thanks a lot your explanation is so clear, and easy to do.

oscaribl
Автор

sir i was searching for this topic only....thnx a lot for sharing ....tkcr

saagarsoni
Автор

Hi Venkat - I regularly watch your videos and i must say this video is so good and explained with so much clarity and in simple language ... Its surely something that people love watching when concepts are explained beautifully and you are surely doing a great job at it ... Thanks :) Keep the good videos and knowledge sharing coming always ...

acklas
Автор

hi your videos are awesome, i have been watching these videos from almost a year. Sir, you are doing a great job. Now to the point, how to preview the pictures before uploading them

sheikhshuaib
Автор

Firstly greetings Sir, Your videos amazing. Please make a videos on file downloading with progress bar...

dineshrana
Автор

Here is the syntax - + fileName). Hope this helps. Good Luck.

Csharp-video-tutorialsBlogspot
Автор

very very very helpful . Thank u very much .

sumitbopche
visit shbcf.ru