2017-05-30 94 views
-1
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(); 
} 

并且它已经无限循环了!任何人都可以帮忙?asp.net - 无限循环foreach

+0

你保存和来自同一个文件夹中读取文件之外? –

+0

这是你在你的代码中设置断点并逐步完成的部分..你是否至少做了这些?..? – MethodMan

+1

是的,正如MethodMan所描述的,您需要先使用调试器。然后在你的代码中有一个重要的性能缺陷。您正在for循环中使用db.SaveChanges(),这会影响应用程序的性能。请检查。 – Varun

回答

0

最后感谢Varun的我把

db.saveChanges(); 

的foreach