我想上传多张图片并将其保存到数据库中。但在Controller动作中:UploadMorePhotos。我在文件中看到了我选择的文件,但如果按此行:file.SaveAs(path);
,它只保存一个图像,然后转到if (ModelState.IsValid)
。那么如何上传您选择的所有图像。谢谢
我有这个:
控制器操作:
[HttpPost]
public ActionResult UploadMorePhotos(UserProfile userProfile, IEnumerable<HttpPostedFileBase>files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
//ModelState.Clear();
}
if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));
// Update fields
user.Image = new byte[file.ContentLength];
file.InputStream.Read(user.Image, 0, file.ContentLength);
user.ImageMimeType = file.ContentType;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
return View(userProfile);
}
public ActionResult GetImage(int id)
{
byte[] image = db.userProfiles.Where(p => p.Id == id).Select(img => img.Image).FirstOrDefault();
//var v = // FirstOrDefault(u => u.Id.Equals(itemId));
// image = v;
return File(image,"image/jpeg");
}
型号:
[DisplayName("Image")]
[MaxLength]
public byte[] Image { get; set; }
[Display(Name = "Display profile Image")]
public bool DisplayItem { get; set; }
[DisplayName("ImageMimeType")]
public string ImageMimeType { get; set; }
public virtual ICollection<LolaBikePhoto> LolaBikePhotos { get; set; }
public class LolaBikePhoto
{
[Key]
public int PhotoID { get; set; }
public int UserProfileID { get; set; }
public string Photo { get; set; }
public string Notes { get; set; }
public virtual UserProfile userProfile { get; set; }
}
和查看:
<div id="tabs-3">
@using (Html.BeginForm("UploadMorePhotos", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-field">
<p>Select pictures:</p>
<div class="upload-container">
<div class="upload">
<input type="file" name="files" value="" multiple="multiple" id="file1" />
<img src="@Url.Content("~/images/deleteButton.png")" alt="Remove picture." />
</div>
</div>
</div>
<div class="form-field">
<input type="submit" value="Create" />
</div>
}
</div>
我现在就这样:
[HttpPost]
public ActionResult UploadMorePhotos(UserProfile userProfile, IEnumerable<HttpPostedFileBase>files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
//ModelState.Clear();
}
if (ModelState.IsValid)
{
string username = User.Identity.Name;
// Get the userprofile
UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));
// Update fields
user.Image = new byte[file.ContentLength];
file.InputStream.Read(user.Image, 0, file.ContentLength);
user.ImageMimeType = file.ContentType;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
//return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
return View(userProfile);
}
但仍然只有一张图片
return View(userProfile); }
return View(userProfile);
}
答案 0 :(得分:0)
让我们把脂肪从你的循环中切除:
foreach (var file in files)
{
if (file.ContentLength > 0)
{
file.SaveAs(path);
}
if (ModelState.IsValid)
{
db.SaveChanges();
}
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
}
您将在第一次迭代结束时从循环返回。
如果删除
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
你的循环应该完成。
<强>更新强>
但我希望在上传图片后留在标签-3上
然后替换
return View(userProfile);
与
return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
确保在完成循环后返回 。