In the following blog post, you will learn about asp.net Upload and download in asp.net c# and how to save image in a folder using file upload in asp.net c#. The FileUpload control was introduced in ASP.NET 2.0.
The FileUpload control allows the user to browse for and select the file to be uploaded, providing a browse button and a text box for entering the filename.
- Keep reading on JavaScript Drag and Drop File Uploader
Upload and Download files from Folder (Directory) in ASP.Net C#
The following example shows how to use an ASP.NET FileUpload control to browse files and upload them to the Web server. Let’s get started by creating a new web application using the visual studio.
To create a control, simply drop the FileUpload control from Toolbox to a Web page. The following code adds FileUpLoad control:
To support file upload, we need to add a Link Button control:
Upload
Now on this button click event handler, we need to call the SaveAs method of FileUpLoad control, which takes a full path where the file will be uploaded. Find the below full source code:-
Upload and Download files from Folder (Directory) in ASP.Net using C#
using System; using System.IO; public partial class _Default : System.Web.UI.Page { string filename = string.Empty; protected void Page_Load(object sender, EventArgs e) { } //To upload file protected void OnLnkUpload_Click(object sender, EventArgs e) { filename = Path.GetFileName(fileUpload1.PostedFile.FileName); fileUpload1.SaveAs(Server.MapPath("Files/" + filename)); lblFilename.Text = "Files/" + fileUpload1.FileName; lblMessage.Text = "File uploaded sucessfully to the folder: -" + " Files/" + fileUpload1.FileName; } // To download uplaoded file protected void OnLnkDownload_Click(object sender, EventArgs e) { if (lblFilename.Text != string.Empty) { if (lblFilename.Text.EndsWith(".txt")) { Response.ContentType = "application/txt"; } else if (lblFilename.Text.EndsWith(".pdf")) { Response.ContentType = "application/pdf"; } else if (lblFilename.Text.EndsWith(".docx")) { Response.ContentType = "application/docx"; } else { Response.ContentType = "image/jpg"; } string filePath = lblFilename.Text; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); Response.TransmitFile(Server.MapPath(filePath)); Response.End(); } } }
Download Source Code
Conclusion
I hope you liked this article on the file upload and download in asp.net. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
The post File Upload and Download in ASP.NET C# appeared first on DotNetTec.
This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here