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:
- Microsoft:Downloading and Uploading File
Other Related and Popular Articles :
Author Profile : Hans Candra
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