控制器代码:
- C# code
// GET: /Admin/AddArticle public ActionResult AddArticle() { ViewBag.AddDate = DateTime.Now; ViewBag.FenleiID = new SelectList(db.Fenleis, "FenleiID", "FenleiName"); return View(); } [HttpPost] [ValidateInput(false)] public ActionResult AddArticle(Article newArticle, HttpPostedFileBase image) { ViewBagtext = new MvcHtmlString("Content"); ViewBag.AddDate = DateTime.Now; int articleid = db.SaveChanges(); if (ModelState.IsValid) { //这是标题图片上传方法 if (image != null) { string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString("00"); string day = DateTime.Now.Day.ToString("00"); string fileFolder = string.Concat("Upload/", year, "/", month, "/", day); string path = HostingEnvironment.MapPath("~/" + fileFolder); if (!Directory.Exists(path)) { //在Upload目录下创建了一个文件夹 Directory.CreateDirectory(path); } string imagename = image.FileName; string fileType = Path.GetExtension(image.FileName).ToLower(); if (fileType == ".jpeg" || fileType == ".jpg" || fileType == ".png" || fileType == ".gif") { Random ran = new Random(); string name = DateTime.Now.ToString("yyyyMMdhhmmss") + ran.Next(9999) + fileType; image.SaveAs(Path.Combine(path, name)); string imageurl = string.Concat(HostingEnvironment.ApplicationVirtualPath, fileFolder, "/", name); newArticle.ImageUrl = imageurl; } } //这是文件上传方法 foreach(string upload in Request.Files) { if (Request.Files[upload].ContentLength<=0) continue; string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString("00"); string day = DateTime.Now.Day.ToString("00"); string fileFolder = string.Concat("Upload/", year, "/", month, "/", day); string path = HostingEnvironment.MapPath("~/" + fileFolder); if (!Directory.Exists(path)) { //在Upload目录下创建了一个文件夹 Directory.CreateDirectory(path); } string filename = Request.Files[upload].FileName; string fileType = Path.GetExtension(Request.Files[upload].FileName).ToLower(); string nickName = Path.GetFileName(Request.Files[upload].FileName); Random ran = new Random(); string name = DateTime.Now.ToString("YYyyMMdhhmmss") + ran.Next(9999) + fileType; Request.Files[upload].SaveAs(Path.Combine(path, name)); AddTempAttachment(nickName, "/" + fileFolder + "/" + name, Convert.ToInt32(Session["userid"])); } db.Articles.Add(newArticle); db.SaveChanges(); //将博文的id转换为字符串 if (Request.Files != null) { try { int aid = newArticle.ArticleID; foreach (var att in GetAllTempAtt()) { AddAttachment(aid, Convert.ToInt32(Session["userid"]), att.tempFilename, att.tempPath, DateTime.Now, "附件 描述"); } DelTempAtt(); //db.SaveChanges(); } catch (Exception ex) { ViewBag.State = ex.Message; } } return RedirectToAction("Index","Home"); } ViewBag.FenleiID = new SelectList(db.Fenleis, "FenleiID", "FenleiName", newArticle.FenleiID); return View(newArticle); }
VIEW文件:
- C# code
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })){ @Html.ValidationSummary(true) <fieldset> <legend><h2>添加博文</h2></legend> <div class="editor-label"> @Html.LabelFor(model => model.FenleiID) </div> <div class="editor-field"> @Html.DropDownList("FenleiID", "请选择") @Html.ValidationMessageFor(model => model.FenleiID) </div> <div class="editor-label"> @Html.LabelFor(model => model.Title) </div> <div class="editor-field"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> <div class="editor-label"> @Html.LabelFor(model => model.SubTitle) </div> <div class="editor-field"> @Html.EditorFor(model => model.SubTitle) @Html.ValidationMessageFor(model => model.SubTitle) </div> <div class="editor-label"> @Html.LabelFor(model => model.ImageUrl) </div> <div class="editor-field"> <div>上传图片: <input type="file" name="image" /></div> @Html.ValidationMessageFor(model => model.ImageUrl) </div> <div class="editor-label"> @Html.LabelFor(model => model.Content) </div> <div class="editor-field"> <textarea name="Content" id="htmlEdtior" cols="100" rows="25"></textarea> @Html.ValidationMessageFor(model => model.Content) </div> <div class="editor-label"> @Html.LabelFor(model => model.Tag) </div> <div class="editor-field"> @Html.EditorFor(model => model.Tag)<span>Tag之间请用英文“<font color="red" size="6">,</font>”分开</span> @Html.ValidationMessageFor(model => model.Tag) </div> <div class="editor-label"> @Html.LabelFor(model => model.Keywords) </div> <div class="editor-field"> @Html.EditorFor(model => model.Keywords) @Html.ValidationMessageFor(model => model.Keywords) </div> <div class="editor-label"> @Html.LabelFor(model => model.Summary) </div> <div class="editor-field"> @Html.TextAreaFor(model => model.Summary) @Html.ValidationMessageFor(model => model.Summary) </div> <div class="editor-label"> @Html.LabelFor(model => model.AddDate) </div> <div class="editor-field"> @Html.TextBoxFor(model => model.AddDate, new { @class = "Wdate", onClick = "WdatePicker({isShowClear:false,readOnly:true,dateFmt:'yyyy-MM-dd HH:mm'});" }) @Html.ValidationMessageFor(model => model.AddDate) </div> <div class="editor-label"> @Html.LabelFor(model => model.Source) </div> <div class="editor-field"> @Html.EditorFor(model => model.Source) @Html.ValidationMessageFor(model => model.Source) </div> <div class="editor-label"> @Html.LabelFor(model => model.IsRemmended) </div> <div class="editor-field"> @Html.CheckBoxFor(model => model.IsRemmended) @Html.ValidationMessageFor(model => model.IsRemmended) </div> <div class="editor-label"> 上传文件: </div> <div class="editor-field"> <input name="file1" id="file1" type="file" /> <input name="file2" id="file2" type="file" /> <input name="file3" id="file3" type="file" /> <input name="file4" id="file4" type="file" /> <input name="file5" id="file5" type="file" /> </div> <p> <input type="submit" value="Create" /> </p> </fieldset>}
想将VIEW中的上传改为类似以下这样的
<div class="editor-label">
上传文件:
</div>
<div class="editor-field">
<input name="file1" id="file1" type="file" />
<input name="file2" id="file2" type="file" />
<input name="file3" id="file3" type="file" />
<input name="file4" id="file4" type="file" />
<input name="file5" id="file5" type="file" />
</div>
<input name="uploadfile" value="上传文件" >
就是加了一段红色的代码,想实现在添加文章之前先上传文件。这个要怎么操作?
------解决方案--------------------------------------------------------
- HTML code
@using (Html.BeginForm("ImportPackage", "Package", FormMethod.Post, new { id = "UpLoadForm", enctype = "multipart/form-data" })){}<form id='formExport' enctype = 'application/x-www-form-urlencoded' action='@Url.Action("ExportToExcel", "Package")'></form>---------这两个才是重点,实现的功能是图片的上传 和 excel 导出设置还可以再添加 form 用于提交,或者修改 UploadForm 的 enctype 后提交