C# - Upload and Download Files From Folder Using ASP.Net

random

C# - Upload and Download Files From Folder Using ASP.Net

File Upload and Download using Asp.net C#

Uploading and downloading files are the most common methods for any website or web application. In this example you will learn how to save/upload files in folder and download files from folder using ASP.net. 

This asp.net tutorial demonstrates how to save the file, upload and download files using FileUpload asp.net web control. The following example is a common practice of file Uploading and downloading from folder system. Find the c# code snippet below.

Upload and download files, and download files, upload and download, using asp net,app data images, download files using, files using asp, save the file, asp net, upload and, file name, download files, form data, to upload, file, upload, method, download, files
Upload File to Server Folder for Download
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Save/Upload files in folder and download files from folder</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="padding20px;">
            <tr>
                <td>
                    <asp:Label ID="lblFilename" runat="server" Text="Browse:"></asp:Label>
                </td>
                <td>
                    <asp:FileUpload ID="fileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td>
                    <asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click" Font-Underline="False">Upload</asp:LinkButton>
                </td>
                <td>
                    <asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click" Font-Underline="False">Download</asp:LinkButton>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
save/upload files in folder and download files from folder in asp.net
asp.net fileupload


To Upload file
file upload in asp.net c# example

Download & Save file to the folder system
download file in asp.net c#


Default.aspx.cs
using System;

using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    /// <summary>
    /// Author etechpulse
    /// To save/upload files in folder and download files from folder in asp.net
    /// </summary>
    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));

        Response.Write("File uploaded sucessfully.");
        lblFilename.Text = "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();         
        }
    }
} 

C# - Upload and Download Files From Folder Using ASP.Net Reviewed by Ravi Kumar on 3:40 PM Rating: 5

10 comments:

  1. file was uploaded but when page is refreshed downlink does not work,,
    what should i do for keeping the file uploaded in the path even if the page is refreshed

    ReplyDelete
  2. I have been searching for this solution for 2 days already.
    Thanks a lot!

    ReplyDelete
    Replies
    1. u're welcome.. :) and keep on reading.

      Delete
  3. i want to give a physical path instead of virtual path ....how can we do this ..???

    ReplyDelete
    Replies
    1. use fileUpload1.SaveAs("D:\\" + filename);

      Delete
  4. thanks....I have been searching for this solution for 2 days already. It is easy to understand and very helpfull for me.

    ReplyDelete
  5. thanks...it is a funtastic blog. I have been searching for this solution for 2 days already

    ReplyDelete

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.