【发布时间】:2017-10-31 03:51:10
【问题描述】:
foreach (HttpPostedFile file in filePhotos.PostedFiles)
{
RandomStringGenerator rnd = new RandomStringGenerator();
string filename = rnd.Generate(5) + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("~/Content/Photos/") + filename);
Sweet_Shailene___EN.Photo p = new Sweet_Shailene___EN.Photo();
p.GalleryId = Convert.ToInt32(drpGallery.SelectedValue);
p.Url = filename;
db.Photos.Add(p);
db.SaveChanges();
}
然后它进入了一个无限循环!有人可以帮忙吗?
【问题讨论】:
-
您是否保存和读取同一文件夹中的文件?
-
这是您在代码中设置断点并单步执行的部分。您是否至少做到了...?
-
是的,正如 MethodMan 所述,您需要先使用调试器。然后,您的代码中存在一个重要的性能缺陷。您在 for 循环中使用 db.SaveChanges() 会影响应用程序的性能。请检查。
-
@Varun 谢谢!我把 db.saveChanges() 放在 foreach 之外,问题就解决了!
标签: c# asp.net file file-upload foreach