selvi jp

selvi jp

  • 1.7k
  • 314
  • 41.4k

Multiple file upload with ASP.NET WebApi

May 8 2021 2:51 AM
Multiple file upload store the file path in sql with ASP.NET WebApi.I can store multiple image on folder. But image path store only once.I want to store image path also multiple time
  1. public string UploadFiles()  
  2. {  
  3.     int iUploadedCnt = 0;  
  4.   
  5.     // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.  
  6.     string sPath = "";  
  7.     sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/");  
  8.   
  9.     System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;  
  10.   
  11.     // CHECK THE FILE COUNT.  
  12.     for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)  
  13.     {  
  14.         System.Web.HttpPostedFile hpf = hfc[iCnt];  
  15.   
  16.         if (hpf.ContentLength > 0)  
  17.         {  
  18.             // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)  
  19.             if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))  
  20.             {  
  21.                 // SAVE THE FILES IN THE FOLDER.  
  22.                 hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));  
  23.                 iUploadedCnt = iUploadedCnt + 1;  
  24.                 //Read the File data from Request.Form collection.  
  25.                 HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];  
  26.   
  27.                 //Insert the File to Database Table.  
  28.                 WebApiDatabaseEntities entities = new WebApiDatabaseEntities();  
  29.                 ImageUpload file = new ImageUpload();  
  30.   
  31.                     file.ImagePath = Path.GetFileName(postedFile.FileName);  
  32.                     file.Title = postedFile.ContentType;  
  33.                     file.CreatedDate = DateTime.Now;  
  34.                    
  35.                 entities.ImageUploads.Add(file);  
  36.                 entities.SaveChanges();  
  37.             }  
  38.         }  
  39.     }  
  40. } 

Answers (1)