asp.net-mvc – 上传文件并验证文件扩展名和文件大小MVC 5

我使用下面的代码,以便上传检查文件扩展名和文件大小

更新2
调节器

public ActionResult Create([Bind(Include = "anak_ID,Pubdate,kind,title,file,details,link")] HttpPostedFileBase file,announcement announcement)
    {
        if (ModelState.IsValid)
        {
            db.announcement.Add(announcement);
            db.SaveChanges();
            TempData["notice"] = "Data saved";

            var allowedExtensions = new[] { ".pdf",".zip",".rar" };

            if (file!= null && file.ContentLength > 0)
            {
                var checkextension = Path.GetExtension(file.FileName).ToLower();


                if (itm.Contains(checkextension))
                    {
                        var extension = Path.GetExtension(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/Content/AnnFiles/" + "announcement_" + announcement.anak_ID + extension));

                        //save File
                        file.SaveAs(path);

                        //prepere announcement
                        announcement.file= @"announcement_" + announcement.anak_ID + extension;


                        //Code for Save data to announcement.

                        db.SaveChanges();
                        TempData["notice"] = "OK! the file is uploaded";
                    }
                    else
                    {

                        TempData["notice"] = "Select pdf or zip or rar less than 20Μ";

                    }

            }

            return RedirectToAction("Create",announcement);


        }

        return View(announcement);
    }

创建视图文件字段.

<div class="form-group">
        @Html.LabelFor(model => model.file,htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-8">
            @Html.EditorFor(model => model.file,new { htmlAttributes = new { @class = "input-file",type = "file",name = "file"} })

        </div>
    </div>

创建视图(我显示消息的部分).

@if (TempData["notice"] != null)
    {
        <div class="alert alert-danger fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            @TempData["notice"]
        </div>
    }

它将记录保存在db中但在文件字段中保存“System.Web.HttpPostedFileWrapper”

当我更改if语句时,问题就开始了

if (file != null && file .ContentLength > 0)

if (file != null && file .ContentLength > 0 && allowedExtensions.Contains(Path.GetExtension(file .FileName).ToLower()) && file .ContentLength <= (20 * 1024))

所以检查文件扩展名和文件大小.

一个问题是它总是显示消息
“选择pdf或zip或rar小于20M”并保存记录.我因为System.Web.HttpPostedFileWrapper值而退出.我想要实现的是当我选择不允许的扩展名和表中的文件名时,不保存记录.
先感谢您

解决方法

看看这些代码.

添加.png进行测试,你可以删除它.

if (ModelState.IsValid)
{ 
   var allowedExtensions = new[] { ".pdf",".rar" };
                var checkextension = Path.GetExtension(file.FileName).ToLower();

                if (!allowedExtensions.Contains(checkextension))
                {
                    TempData["notice"] = "Select pdf or zip or rar less than 20Μ";
                }

                foreach (var itm in allowedExtensions)
                {
                    if (itm.Contains(checkextension))
                    {
                        db.announcement.Add(announcement);
                        dbo.SaveChanges();
                    }
                }

                if (file != null && file.ContentLength > 0)
                {
                    foreach (var itm in allowedExtensions)
                    {
                        if (itm.Contains(checkextension))
                        {
                            var extension = Path.GetExtension(file.FileName);
                            var path = Path.Combine(Server.MapPath("~/Content/AnnFiles/" + "announcement_" + announcement.anak_ID + extension));

                            //save File
                            file.SaveAs(path);

                            //prepere announcement
                            announcement.file = @"announcement_" + announcement.anak_ID + extension;


                            //Code for Save data to announcement.

                            db.SaveChanges();
                            TempData["notice"] = "OK! the file is uploaded";
                        }
                    }

                }
            }
}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....