How to upload and download the files from the web server


HTML clipboard

There are number of times the user wants to upload some file and want the way to download the files from the server. ASP.net provides the FileUpload control to upload the file to the web server. It provides the easy way to upload the file to the server; the coder has not to write whole logic to read the files and writes to the web server.

Here I will mention how to use the File Upload control and upload the files to the server. I have added one more button to download the file from the server. Response Object provides the AddHeader method where you can mention the different type of Header passed to the client.
Here I have use the content-disposition and attachment attribute, where you can specify the file name which you want to pass to the client. On client web browser will prompt the user to download the file.

Client Side Code: 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></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table border="1" cellspacing="0" cellpadding="0" id="tbl">
            <tbody>
                <tr>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="Solid" ForeColor="Black"
                            Width="329px" BackColor="White" />
                    </td>
                </tr>
            </tbody>
        </table>
        <table border="1" cellspacing="0" cellpadding="0" id="tblButton">
            <tbody>
                <tr>
                    <td>
                        <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="btnUpload_Click" />
                    </td>
                    <td>
                        <asp:Button ID="btnDownload" Text="Download" runat="server" OnClick="btnDownload_Click" />
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    </form>
</body>
</
html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        String filePath = FileUpload1.FileName;
        String strFileName = "";

        if (FileUpload1.PostedFile != null)
        {
            HttpPostedFile file = FileUpload1.PostedFile;
            //Get the size of the file so you can read the file
            int contentLen = file.ContentLength;
            if (contentLen > 0)
            {
                strFileName = Path.GetFileName(filePath);
                file.SaveAs(Server.MapPath(strFileName));
            }
        }
    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        string fileName = "Amazon.txt";
        string filePath = Server.MapPath(fileName);
        Response.Clear();

        Response.AppendHeader("content-disposition", "attachment; filename=" + filePath);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath);
        Response.Flush();
        Response.End();
    }
}

Hope this will guide how to upload and download the files from the web server.
 


Similar Articles