protected System.Web.UI.HtmlControls.HtmlInputFile file;in upload button click event handling function write this codeif (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"); } }
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."); } } }
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."); } }