WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 108 users


WorldofASP.NET >> ASP.NET >> Unedited ASP.NET

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here

Simple Method to Upload and Download File Attachment in ASP.NET

How to post file on server and download using streaming dialog box
Published Date : 04 Aug 2008
Author : Hans Candra
Language : C#
Platform : .NET
Technology : ASP.NET,Web Forms
Views : 1445
Rating : (0 votes so far)


Introduction

File upload and download are a simple tasks in ASP.NET using the extensive .NET Framework class library. File upload in ASP.NET is as easy as retrieving an HTML form value. File download is even easier in ASP.NET with the new WriteFile method in the Request object.

Main

Sending a file to the browser can be achieved using a single line of code. Many Web programmers still wonder how they can force the browser to display the Download dialog. Sending the file with the WriteFile method will not necessarily display the dialog.

File Download

To force the browser to display the Download dialog, you need to set the Response object's ContentType property to application/octet-stream and add an HTTP Request header called Content-Disposition with the following value: attachment; filename="filename" where filename is the default filename that will be displayed in the Download dialog at the browser. Therefore, to send a file to the browser, the first thing to do is to write the following code: 


Code on download aspx page:

 <asp:linkbutton id="lnkAttachment" runat="server" visible="false" 
onclick="lnkAttachment_Click">
                        <asp:image id="imgAttachment" runat="server" 
tooltip="View Attachment File" imageurl="~/Picture/clipboard.gif">
                    </asp:image>
</asp:linkbutton>

Code snippet below is raised when you click a linkattachment in the page.

    protected void lnkAttachment_Click(object sender, EventArgs e)
    {
        string sAttachmentFileName = "AttachmentFileName";
        SecureQueryString sqs = new SecureQueryString();
        sqs["Attachment"] = sAttachmentFileName;
        DownloadAttachment();
    }
    private void DownloadAttachment()
    {
        string sAttachmentFileName = "AttachmentFileName";
        string sFilePath = "D:\\UploadFolder" + "\\" + sAttachmentFileName;
        Response.Clear();

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

File Upload

To perform file upload in ASP.NET, you need to know two classes: the System.Web.UI.HtmlControls.HtmlInputFile class and the System.Web.HttpPostedFile. The first class represents an HTML control that the user uses to select a file on the client side and upload it. The latter represents the uploaded file itself, and an instance of this class is obtained from the HtmlInputFile control. The next two subsections discuss each class in detail.

Code on upload.aspx page:

       

 
  Attachment: <asp:fileupload id="FUpload" runat="server">
</asp:fileupload>
 

HttpPostedFile AttachmentFile = Request.Files[0];
            if (AttachmentFile != null && AttachmentFile.FileName.Length > 0)
            {
                string sFileName = Path.GetFileName(AttachmentFile.FileName);
                string sFileExtension = Path.GetExtension(sFileName);
                string sNewFileName = iNewQueryID + sFileExtension;
                AttachmentFile.SaveAs(@"D:\UploadFolder\" + sNewFileName);
}

Conclusion

File upload is become simple in using ASP.NET. The first section of this article explained how to write code to download a file, and the second section discussed programmable file upload. The information that you gain here in two section hopefully will be a good addition to your asp.net knowledge.

References

Include all the useful links or references that can help users learn about this tutorial:

  1. Microsoft:Downloading and Uploading File


Other Related and Popular Articles :

Simple asp2 feedback form
A simple way to get user feedback via webpage email.

Designing a Membership Sign-Up and Login Page in ASP.NET
How to design user interface for registration and login in ASP.NET

Validate Email and Domain in a Web Form
Simple method to validate using javascript and ASP.NET validation control

Basics of XHTML-MP
Basics of XHTML-MP

Activate User Registration by Email in ASP.NET
How to activate registered user using System.Net.Mail.SMTPClient class

Implement captcha control library for ASP.NET
a captcha control, image, image handler, and supression class for form validation


Author Profile : Hans Candra

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

Leave New Comments


Article Content copyright by Hans Candra
Everything else Copyright © by WorldofASP.NET 2008





Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card