博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC3 上传文件
阅读量:7016 次
发布时间:2019-06-28

本文共 2373 字,大约阅读时间需要 7 分钟。

注意:红色部分必须添加 

前台:

@{
    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();

        } 

转载于:https://www.cnblogs.com/94cool/archive/2012/11/02/2750672.html

你可能感兴趣的文章
Android 左右滑屏效果
查看>>
类方法代码重构-寻找坏味道
查看>>
析构函数构造函数CPerson派生出CEmployee类
查看>>
安装文件在Ubuntu12.04上部署CloudFoundry-ng (一) dea_ng和warden的部署
查看>>
SkinSharp For C# .Net 2005/2008/2010 使用帮助
查看>>
华夏工程dom4j (2) 编辑
查看>>
Metro Studio 2.0.1.5
查看>>
MSSQL有关时间函数知识(转)
查看>>
Windows Phone 更改datePicker的显示格式
查看>>
JSP和JSTL获取服务器参数
查看>>
lxml.etree 教程5:Using XPath to find text
查看>>
Python学习入门基础教程(learning Python)--2.2 Python下的变量基础
查看>>
修改mysql root账户登录密码
查看>>
MDT概念说明
查看>>
祖国版SoloWheel:Airwheel爱尔威火星车 拆箱&上手经验_运动户外_晒物广场_什么值得买...
查看>>
【原】浅谈测试和产品
查看>>
tomcat 容器生命周期lifecycle
查看>>
VC调用javascript的几种方法
查看>>
Entity Framework简介
查看>>
图片轮播小列子
查看>>