• MVC上传文件


    代码

    <form class="cmxform form-horizontal tasi-form" id="submitForm" method="POST" action="Add" novalidate="novalidate" enctype="multipart/form-data">
                            @{
                                if (!ViewData.ModelState.IsValid)
                                {
                                    <div class="alert alert-danger alert-dismissable">
                                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                                        @string.Format("操作失败:{0}", ViewData.ModelState["Error"].Errors[0].ErrorMessage)
                                    </div>
                                }
                            }
    
                            <div class="form-group ">
                                @Html.LabelFor(item => item.Name, new { @class = "control-label col-lg-2" })
                                <div class="col-lg-10">
                                    @Html.TextBoxFor(item => item.Name, new { @class = "form-control"})
                                </div>
                            </div>
                            <div class="form-group ">
                                @Html.LabelFor(item => item.LinkUrl, new { @class = "control-label col-lg-2" })
                                <div class="col-lg-10">
                                    @Html.TextBoxFor(item => item.LinkUrl, new { @class = "form-control" })
                                </div>
                            </div>
    
                            <div class="form-group ">
                                @Html.LabelFor(item => item.Img, new { @class = "control-label col-lg-2" })
                                <div class="col-lg-10">
                                    <input type="file" name="file" class="form-control" />
                                </div>
                            </div>
    
                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-10">
                                    <button class="btn btn-success" type="submit">保存</button>
                                    <button class="btn btn-default" type="button" onclick="window.history.go(-1);">返回</button>
                                </div>
                            </div>
                        </form>

    注意:form里要加入 enctype="multipart/form-data",说明有文件要提交。

    后台对应的要加入HttpPostedFileBase file, file 对应的是html中的上传控件的name属性。

    后台:

    [HttpPost]
            public ActionResult Add(Service.Dto.BannerInfoDto dto, HttpPostedFileBase file)
            {
                string fileName = "";
    
                if (file == null)
                {
                    ModelState.AddModelError("Error", "请选择图片 .jpg/.png/.gif");
                    return View(dto);
                }
    
                if (file != null)
                {
                    string fileExt = Path.GetExtension(file.FileName).ToLower();
                    if (!".jpg/.png/.gif".Contains(fileExt))
                    {
                        ModelState.AddModelError("Error", "图片格式不正确 .jpg/.png/.gif");
                        return View(dto);
                    }
    
                    fileName = "~/UploadFile/BannerInfo/" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
    
                    string serPath = Server.MapPath(fileName);
    
                    try
                    {
                        file.SaveAs(serPath);
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("Error", "上传异常:" + ex.Message);
                        return View(dto);
                    }
                }
    
                Domain.RL_BannerInfo model = new Domain.RL_BannerInfo();
                model.LinkUrl = dto.LinkUrl;
                model.Name = dto.Name;
                model.Img = fileName;
    
                bool result = BannerInfoManage.Save(model); ;
                if (result)
                {
                    return RedirectToAction("Manage");
                }
                else
                {
                    ModelState.AddModelError("Error", "保存失败");
                    return View(dto);
                }
            }

    还有一种方法:Request.Files,这个会得到所有页面上的file上传控件。

    HttpPostedFileBase file = Request.Files[0];如果有多个file,循环获得。
    这样在Action中就不需要加多一个HttpPostedFileBase参数了。

  • 相关阅读:
    分享:TreeFrog 1.3 发布,基于 C++/QT 的 Web 框架
    Linux环境进程间通信(五): 共享内存(上)
    TUP第二期:架构师王鹏云演讲实录 _业界_科技时代_新浪网
    发布我的倒排索引 C/C++ ChinaUnix.net
    操作系统内存管理——分区、页式、段式管理
    内存管理内幕
    IT农民工如何来美国工作
    来自 王斌 (@iwangbin) 的推文
    ScheduledExecutorService执行周期性或定时任务
    PHP XML parse error: Extra content at the end of the document
  • 原文地址:https://www.cnblogs.com/xsj1989/p/7000112.html
Copyright © 2020-2023  润新知