MVC3中的文件上传

时间:2013-04-27 07:26:44

标签: c# asp.net-mvc

我在MVC3中实现多个文件上传,我已经成功实现了,现在我必须在数据库中插入值,在我的Model类中,我有一个List,所以我在插入数据库时​​遇到问题是它插入了多个条目,我已经修复了这个问题,现在出现的问题是只有第一个图像插入数据库.Below是我的代码

 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);
        }

它打破了循环但是在打破之后,当谈到foreach(文件中的var文件)时,它每次都拍摄第一张图像。如何处理这个问题

1 个答案:

答案 0 :(得分:0)

首先,我认为你有错误的方法来保存数据库中的数据。您不需要循环文件列表以保存在数据库中,因为您可以将XML格式字符串中的文件发送到sql server,然后在sql server中可以使用单个查询插入数据。假设您在c#:

中生成了这样的XML字符串
string files=   "<files>
  <file>
     file1 path
  </file>
  <file>
     file2 path
  </file>
  <file>
      file3 path
  </file>
     .
     .
     .
   <file>
      file4 path
  </file>
</files>";

现在在存储过程中首先声明一个XML变量来保存这个xml字符串,然后像这样编写instert查询:

@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)  

使用上述解决方案,您可以只使用一个查询插入整个文件列表,而无需在服务器端循环列表。 希望这会对你有所帮助。