Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir/Mam,
Hi,
i have created a website. I have option for Upload and download in the site.
I want to know the ASP.NET code for upload and download. i have taken upload control from the toolbox so i can select the desired file from browae window. but to upload it to a default path i need code.
plz help me out on this.

Thank you.
Posted
Comments
Nilesh Patil Kolhapur 20-Feb-12 4:46am    
are u uploading image file only or all type of files
Member 8660975 20-Feb-12 4:53am    
all type of file.. lilke articles or documents or anything
The Doer 20-Feb-12 6:00am    
U HAVE GOT ENOUGH SOLUTIONS DIVYA...:)

for file upload

C#
if (FileUploadControl.HasFile)
              {
                  try
                  {
                      filename = Path.GetFileName(FileUploadControl.FileName);
                      FileUploadControl.SaveAs(Server.MapPath("~/Documents/LetterInward/") + filename);
                      objInwardLetter.strPath = "~/Documents/LetterInward/" + filename;
                      StatusLabel.Text = "Upload status: File uploaded!";

C#




FOR DOWNLOADING FILE





C#
protected void lnkAttachment_Click(object sender, EventArgs e)
   {
       string filename = string.Empty;
       string path = "~/Documents/LetterInward/" + lblcurentFilePath.Text;
       if (lblcurentFilePath.Text != "")
       {
           if (path != "")
           {
               filename = (path).Remove(0, (path.LastIndexOf('/') + 1));
               System.IO.FileInfo file;
               file = new System.IO.FileInfo(Server.MapPath(path));
               Response.Clear();
               Response.ClearContent();
               Response.ClearHeaders();
               //Added later on 22-Nov-2011 because zip file not extracted at client side
               Response.Buffer = true;
               Response.AppendHeader("Content-Length", file.Length.ToString());
               Response.ContentType = GetMimeTypeByFileName(filename);
               Response.AppendHeader("Content-Disposition:", "attachment; filename=" + filename);
               Response.TransmitFile(file.FullName);
               Response.Flush();
               ApplicationInstance.CompleteRequest();
           }
       }
   }
   public string GetMimeTypeByFileName(string sFileName)
   {
       string sMime = "application/octet-stream";

       string sExtension = System.IO.Path.GetExtension(sFileName);
       if (!string.IsNullOrEmpty(sExtension))
       {
           sExtension = sExtension.Replace(".", "");
           sExtension = sExtension.ToLower();

           if (sExtension == "xls" || sExtension == "xlsx")
           {
               sMime = "application/ms-excel";
           }
           else if (sExtension == "doc" || sExtension == "docx")
           {
               sMime = "application/msword";
           }
           else if (sExtension == "ppt" || sExtension == "pptx")
           {
               sMime = "application/ms-powerpoint";
           }
           else if (sExtension == "rtf")
           {
               sMime = "application/rtf";
           }
           else if (sExtension == "zip")
           {
               sMime = "application/zip";
           }
           else if (sExtension == "mp3")
           {
               sMime = "audio/mpeg";
           }
           else if (sExtension == "bmp")
           {
               sMime = "image/bmp";
           }
           else if (sExtension == "gif")
           {
               sMime = "image/gif";
           }
           else if (sExtension == "jpg" || sExtension == "jpeg")
           {
               sMime = "image/jpeg";
           }
           else if (sExtension == "png")
           {
               sMime = "image/png";
           }
           else if (sExtension == "tiff" || sExtension == "tif")
           {
               sMime = "image/tiff";
           }
           else if (sExtension == "txt")
           {
               sMime = "text/plain";
           }
       }
       return sMime;
   }
 
Share this answer
 
Hey,

to Save use
FileUpload1.SaveAs(Server.MapPath("~/App_Data/")+FileUpload1.FileName);
best Luck
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900