到目前为止,我可以按图像上传图片并将其保存在数据库中,但由于我有几张图片,我需要一次性上传文件夹中包含的所有图片。 这是我现在的控制器:
[HttpPost]
public ActionResult Form(HttpPostedFileBase file, DateTime dateParution, long IdJournal, string numEditionJournal)
{
var db = new Bd_scanitEntities();
IEnumerable<SelectListItem> items = db.JournalSet
.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.label
}).OrderBy(c => c.Text);/*_*/
ViewBag.IdJournal = items;
ScanITAPP.Service.ImageRender service = new Service.ImageRender();
service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);
return RedirectToAction("Index");
}
类ImageRender
public void UploadImageToDB(HttpPostedFileBase file, DateTime dateParution, long IdJournal, string numEditionJournal)
{
AnnonceVImgSet annImg = new AnnonceVImgSet();
annImg.Id = 1;
ImgOrgSet img = new ImgOrgSet();
img.User_Id = 1;
img.Journal_Id = IdJournal;
img.dateModif = DateTime.Now;
img.dateParution = dateParution;
img.dateSaisi = DateTime.Now;
img.numEditionJournal = numEditionJournal;
img.image = ConvertToBytes(file);
using (Bd_scanitEntities dbContext = new Bd_scanitEntities())
{
dbContext.ImgOrgSet.Add(img);
dbContext.SaveChanges();
}
}
public byte[] ConvertToBytes(HttpPostedFileBase Image)
{
byte[] image = null;
BinaryReader reader = new BinaryReader(Image.InputStream);
image = reader.ReadBytes((int)Image.ContentLength);
return image;
}
观点:
@using (Html.BeginForm("Form", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 id="myModalAlertLabel" class="modal-title">Import</h4>
</div>
<div class="modal-body">
<table cellpadding="2" cellspacing="10">
<tr>
<td>
Choisir journal :
</td>
<td>
@Html.DropDownList("IdJournal")
</td>
</tr>
<tr>
<td>
Numéro de l'édition:
</td>
<td>
<input type="text" name="numEditionJournal" />
</td>
</tr>
<tr>
<td>
Date de parution:
</td>
<td>
<input class="form-control span2" name="dateParution" size="16" type="date" value="12-02-2014" >
</tr>
<tr>
<td>
Choisirr image:
</td>
<td>
<input type="file" name="file" id="upload"/>
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="submit" id="load" class="btn btn-primary">Confirmer</button>
</div>
</div>
</div>
}
答案 0 :(得分:2)
将HttpPostedFileBase file
转换为列表并更新您的视图以允许多次上传,如下所示
<强>控制器强>
[HttpPost]
public ActionResult Form(List<HttpPostedFileBase> files, DateTime dateParution, long IdJournal, string numEditionJournal)
{
var db = new Bd_scanitEntities();
IEnumerable<SelectListItem> items = db.JournalSet
.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.label
}).OrderBy(c => c.Text);/*_*/
ViewBag.IdJournal = items;
ScanITAPP.Service.ImageRender service = new Service.ImageRender();
foreach(HttpPostedFileBase file in files){
service.UploadImageToDB(file, dateParution, IdJournal, numEditionJournal);
}
return RedirectToAction("Index");
}
然后,您可能需要修改视图才能允许多次上传
.... <input type="file" multiple="multiple" name="files" id="upload"/>
因为你的文件可能很大, 不要忘记增加网页配置文件中的请求长度。
***编辑****输入类型应该是文件和MVC将用于自动参数的名称文件之间存在错误