简体   繁体   中英

File Upload in MVC3

I am implementing Multiple file upload in MVC3, I have implemented it successfully, now I have to insert values in database as well,In my Model class, I have a List, SO i was having problem in inserting in Database is that its inserting multiple entries, I have fixed this issue, now issue is arising that only first image is inserting in database.Below is my code

 public ActionResult CreateCard(FlashCardsModel model, IEnumerable<HttpPostedFileBase> files)
        {
            string path = "";
            string upath = ConfigurationManager.AppSettings["imgpath"];          
            long SetId = 0;
            for (int i = 0; i < model.GroupIds[0].Split(',').Length; i++)
            {
                model.Visibleto = Convert.ToInt32(model.Privacy);
                var groupid = Convert.ToInt64(model.GroupIds[0].Split(',')[i]);
                SetId = Repository1.CreateSet(model.Title, model.Description, model.Visibleto, Convert.ToInt64(Session["uid"].ToString()), groupid);
                if (SetId != 0)
                {                    
                    foreach (var item in model.CardContent)
                    {
                        foreach (var file in files)
                        {
                            if (file != null && file.ContentLength > 0)
                            {
                                if (file.ContentLength > 0)
                                {
                                    var fileName = Path.GetFileName(file.FileName);
                                    if (IsImage(file.FileName.ToString()))
                                    {
                                        fileName = "FlashCard_Image_" + Session["uid"].ToString() + "_" + fileName;
                                        path = Path.Combine(Server.MapPath(upath), fileName);
                                        file.SaveAs(path);

                                        item.ImagePath = fileName;
                                        if (item.Answer != null && item.Term != null)
                                        {
                                            var savecontent = Repository1.AddCardContent(item.Term, item.Answer, item.ImagePath, SetId);
                                            break;//It breaks the loop but when it comes to foreach (var file in files) its taking first image each time
                                        }                                        
                                    }
                                    else
                                    {
                                        TempData["Errors"] = "Only JPEG,GIF and PNG images are allowed";
                                        return View("CreateFlashCardSet", model);
                                    }
                                }
                            }                          

                        }
                    }              
                }
            }

            TempData["Success"] = "Card Created Successfully";
            return View("CreateFlashCardSet", model);
        }

It breaking the loop but after breaking, when it comes to foreach (var file in files) its taking first image each time. How to handle this issue

First of all, I think you having wrong approach to save the data in database. You do not need to loop the list of files to save in database as you can send the files in XML format string to sql server and then in sql server you can insert the data using a single query. Suppose you have generated XML string like this in c#:

string files=   "<files>
  <file>
     file1 path
  </file>
  <file>
     file2 path
  </file>
  <file>
      file3 path
  </file>
     .
     .
     .
   <file>
      file4 path
  </file>
</files>";

now in store procedure first declare an XML variable to hold this xml string and then write instert query like this:

@FileId int,
@FileNamesXML xml

      INSERT INTO tablename(FileId, FileName ) SELECT @FileId,FileList.value('.[1]', 'varchar(500)') AS FileNames       
FROM @FileNamesXML.nodes('//FileNames/FileName') AS R(FileList)  

With the above solution you can insert the whole list of files with just a single query without looping the list on server side. Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM