ASP.NET File Upload and Download

ASP.NET File Uploader is an easy to use control. Just drag and drop the control on your page and write this code snippet at Button Click Event to upload the file.


if (studentFileUpload.HasFile == true)
{
  string filename = Path.GetFileName(studentFileUpload.FileName);
  studentFileUpload.SaveAs(Server.MapPath("~/DataFile/") + filename);
}



To download file use this code snippet Link Button or Button Click event.


protected void DownloadLinkButton_Click(object sender, EventArgs e)
{
   string fileName = fileNameLabel.Text;
   string filePath = Server.MapPath("~/DataFile/" + fileName + ".txt"); 
   HttpContext.Current.Response.ContentType =  "application/octet-stream";
   HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" +     System.IO.Path.GetFileName(filePath));
   HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.WriteFile(filePath);
   HttpContext.Current.Response.End();
}

Thanks
A Rahim Khan

Advertisements
Comments are closed.
%d bloggers like this: