FileUpload Class: The fileupload class display a textbox and browse button that enable user to select an file on client side and upload it to the web server. Either user can type full file path in textbox or can select file by clicking the browse button and locating file it in the choose file dialog box.
The Fileupload control does not automatically save a file to server after user have selects files to upload. you must explicit provide a control to allow user to upload specified file.For example , you can provide button that the user click to upload the file.
Write below code in update button click
Protected void btnUpload_Click(object sender, EventArgs e)
{
if (( FileUpload1.PostedFile!=null) && FileUpload1.PostedFile.ContentLength >0)
{
String fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
// Create folder in server and name it as “Upload files”. so the user can upload their file in this location
String saveLocation = String.Concat(Server.MapPath(“Upload files”), “\\”, fileName);
}
try {
if (FileUpload1.HasFile) {
FileUpload1.PostedFile.SaveAs(saveLocation);
Response.Write(“The file has been uploaded.”);
}
catch (Exception ex) { Response.Write(“Error: ” + ex.Message);}
}
Before calling SaveAs method to save the file to the server, use the HasFileproperties to verfiy that the fileupload contains a file. if the HasFile return true,call the saveas method,it return false means fileupload does not contain file.
SaveAs method writes the upload file to specified directory, so that create directory for example “Upload files” give the directory path in SaveAs method as parameter. application must have writing access to the directory on the server.
limit the size of the files that can be uploaded by using the FileUpload control. You should set a size limit that is appropriate for the types of files that you expect to be uploaded. The default size limit is 4096 kilobytes (KB), or 4 megabytes (MB). To increase the maximum allowable file size for the entire application, set the maxRequestLengthattribute in the Web.config file. To increase the maximum allowable file size for a specified page, set the maxRequestLength attribute inside the locationelement in Web.config.
For Example
<system.web><httpRuntime maxRequestLength =“90000“/> </system.web>
Download Files Coding
Response.Clear()
Response.AddHeader(“Content-Disposition”, “attachment;filename=” & file.Name)
Response.WriteFile(file.FullName)