C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Uploading and Downloading files using ASP.NET


Posted Date: 29 May 2006    Resource Type: Articles    Category: .NET Framework
Author: phanindra yerraMember Level: Gold    
Rating: Points: 14



Introduction



Introduction

With the ASP.NET file uploading has become an easier without any third party tools . By using html file control and writing a few lines of c# code is enough. This article provides you code for uploading and downloading the files.

If there is only a single file control then we can use the PostedFile property of the file control to upload files. This property is of type HttpPostedFile class and contains the following methods and properties which aids file uploading.
FileName -- Returns the full path and name of the uploaded file.
ContentType -- Returns the full path and name of the uploaded file
ContentLength -- The size in bytes of the uploaded file.
Methods used are SaveAs(Path) Saves the uploaded file to the specified path.
First paste this inside form tag for file field


next declare that html control field in your webform class as



protected System.Web.UI.HtmlControls.HtmlInputFile file;

in upload button click event handling function write this code

if (file.PostedFile !=null) //Checking for valid file
{
/* FileName property gives the entire path but we need only file name so here substring is used. */

string _fileName = file.PostedFile.FileName.Substring(file.PostedFile.FileName.LastIndexOf("\\") + 1) ;
string _fileType = file.PostedFile.ContentType ;
int _fileSize = file.PostedFile.ContentLength;

/*Checking for the length of the file. If length is 0 then file is not uploaded.*/

if (_fileSize<=0)
Response.Write("File Uploading failed for" + _fileName );
else
{
file.PostedFile.SaveAs(Server.MapPath(".\\" + _fileName));
Response.Write( "Your File " + _fileName + " of Type " + _fileType + " and size " + _fileSize.ToString() + " bytes was uploaded successfully");
}


}




If we have multiple file controls within the form we can use the Files property of the Request object which returns a reference to the HttpFileCollection class. HttpFileCollection class has an Item property through which gets individual HttpPostedFile from the file collection either by specifying the name or index.

In update event handle write this code


int IntLoop=0;

//Iterating through the Request.Files collection

for(IntLoop=0;IntLoop {
if (Request.Files[IntLoop] !=null) //Checking for valid file
{
/* Since the FileName gives the entire path we use Substring function to rip of the filename.*/

string StrFileName = Request.Files[IntLoop].FileName.Substring(Request.Files[IntLoop].FileName.LastIndexOf("\\") + 1) ;
string StrFileType = Request.Files[IntLoop].ContentType ;
int IntFileSize = Request.Files[IntLoop].ContentLength;

/*Checking for the file length. If length is 0 then file is not uploaded.*/

if (IntFileSize <=0)
Response.Write(" Uploading of file " + StrFileName + " failed.
");
else
{
/*Saving the file to the web server*/
Request.Files[IntLoop].SaveAs(Server.MapPath(".\\" + StrFileName));
Response.Write( "Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " was uploaded successfully.
");
}
}
}


Downloading file from server

Downloading is also very easy in asp .net just write code like this


string filename = TextBox1.Text;
if (filename != "")
{

string path = Server.MapPath(filename);

System.IO.FileInfo file = new System.IO.FileInfo(path);

if (file.Exists)

{

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

Response.ContentType = "application/octet-stream";

Response.WriteFile(file.FullName);

Response.End();

}

else

{

Response.Write("This file does not exist.");

}

}







Summary




This article explained how to download and upload files from and to the server respectively.







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Use ASP.NET to upload files  .  Use ASP.NET to download files  .  Use ASP.NET to download and upload files  .  Upload files using ASP.NET  .  Upload and download files using ASP.NET  .  Download files using ASP.NET  .  Download and upload files using ASP.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Interview Question (.Net Framework)
Previous Resource: How to upload a file to a Web server in ASP.NET by using c# (C Sharp)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use