注意:红色部分必须添加
前台:
@{
ViewBag.Title = AutoUpdater.Profile.title + " - 上传升级文件";
}
@model AutoUpdater.Models.UploadFileModel
<h2>上传升级文件</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true)
<div class="validation-summary-errors">@ViewBag.ErrorMessage</div><br />
@using (Html.BeginForm("UploadFile", "Operations", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
<fieldset>
<legend>升级文件信息</legend>
<div class="editor-label">
@Html.LabelFor(m => m.Version)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Version)
@Html.ValidationMessageFor(m => m.Version)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.UFile)
</div>
<div class="editor-field">
<input type="file" id="upfile" name="upfile" />
</div>
<p>
<input type="submit" value="上 传" />
</p>
</fieldset>
</div>
}
后天:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFile(FormCollection collection, UploadFileModel model)
{
if (Session["UserID"] == null)
{
ViewBag.ErrorMessage = "请先登录!";
return View();
}
if (Request.Files.Count == 0)
{
ViewBag.ErrorMessage = "请选择上传的升级文件!";
return View();
}
var fileVersion = Request.Files[0];
if (fileVersion == null || (fileVersion != null && fileVersion.ContentLength == 0))
{
ViewBag.ErrorMessage = "请选择上传的升级文件!";
return View();
}
if (fileVersion != null && fileVersion.ContentLength > 0)
{
if (fileVersion.ContentLength < Profile.minLen || fileVersion.ContentLength > Profile.maxLen)
{
ViewBag.ErrorMessage = string.Format("上传的升级文件必须介于{0}K-{1}K之间!", Profile.minLen / 1024, Profile.maxLen / 1024);
return View();
}
using (var db = new LogDB(Profile.dbpath))
{
if (db.ExistVersion(model.Version.ToUpper()))
{
ViewBag.ErrorMessage = "已经存在相同版本号的升级文件!";
return View();
}
}
string suffix = Path.GetExtension(fileVersion.FileName).ToLower();
string fileName = string.Format("{0}{1}", model.Version.ToUpper(), suffix);
fileVersion.SaveAs(Path.Combine(Profile.filepath, fileName));
using (var db = new LogDB(Profile.dbpath))
{
db.Write(fileName, Session["UserID"].ToString(), fileVersion.ContentLength, model.Version.ToUpper());
}
return RedirectToAction("FileList", "Operations");
}
return View();
}