关于asp.net mvc:上传文件并验证文件扩展名和文件大小MVC 5

Upload file and validate file extension and file size MVC 5

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

更新 2
控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 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);
    }

创建查看文件字段。

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

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

1
2
3
4
5
6
7
  @if (TempData["notice"] != null)
    {
       
           
            @TempData["notice"]
       
    }

它将记录保存在数据库中,但在文件字段中保存"System.Web.HttpPostedFileWrapper"

当我从

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

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

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

so 检查文件扩展名和文件大小。

另一个问题是它总是显示消息
"选择 pdf 或 zip 或 rar 小于 20??"并保存记录。我问因为 System.Web.HttpPostedFileWrapper 值。我想要实现的是,当我选择不允许的扩展名和表中的文件名时,不保存记录。
提前致谢


看看这些代码。

添加了 .png 用于测试,您可以将其删除。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var allowedExtensions = new[] {".pdf",".zip",".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";
        }
    }
}