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

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

注意:红色部分必须添加 

前台:

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

        } 

本文转自94cool博客园博客,原文链接http://www.cnblogs.com/94cool/archive/2012/11/02/2750672.html,如需转载请自行联系原作者

你可能感兴趣的文章
SessionStateStoreProviderBase.GetItemExclusive Method
查看>>
Excel Wrapper
查看>>
Thread和Service应用场合的区别
查看>>
poj 2632 模拟题
查看>>
递归--木棍问题
查看>>
企业门户(Portal)项目实施方略与开发指南
查看>>
基于百度定位SDK的定位服务的实现
查看>>
是你的,就是你的。越是紧握,越容易失去。
查看>>
[LeetCode] Implement Stack using Queues
查看>>
cherrypy安装使用,配置python环境变量
查看>>
MVC验证12-使用DataAnnotationsExtensions对整型、邮件、最小值、文件类型、Url地址等验证...
查看>>
Source not found
查看>>
【CLRS】《算法导论》读书笔记(一):堆排序(Heapsort)
查看>>
支持类型过滤的枚举器
查看>>
HDU 4275 Color the Tree(树同构)
查看>>
php里Array2xml
查看>>
以boost::function和boost:bind取代虚函数
查看>>
oracle监听器(listener)配置心得
查看>>
wince -- RS485半双工实现
查看>>
nginx 源码学习笔记(二)——nginx精粹-模块
查看>>