ASP.NETMVC:图像上传列
创始人
2024-09-19 03:31:15
0
  1. 在模型中添加一个属性来存储图像文件:
public class MyModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public HttpPostedFileBase ImageFile { get; set; }
}
  1. 在视图中使用表单和文件上传控件来上传图像:
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {
    @Html.AntiForgeryToken()

    

MyModel


@Html.ValidationSummary(true)
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.ImageFile, htmlAttributes: new { @class = "control-label col-md-2" })
}
  1. 在控制器中处理图像上传并将其保存到磁盘:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(MyModel model)
{
    if (ModelState.IsValid)
    {
        if (model.ImageFile != null && model.ImageFile.ContentLength > 0)
        {
            var fileName = Path.GetFileName(model.ImageFile.FileName);
            var path = Path.Combine(Server.MapPath("~/Images"), fileName);
            model.ImageFile.SaveAs(path);
        }

        // Save your model to the database and redirect to a success page
        // ...

        return RedirectToAction("Index");
    }

    return View(model);
}

相关内容

热门资讯

发现一款(WePoKe好友局)... 发现一款(WePoKe好友局)外挂透明挂辅助软件(黑科技)管理员(有挂方式)-哔哩哔哩是一款可以让一...
热点推荐!反杀大厅辅助,wpk... 热点推荐!反杀大厅辅助,wpk免费辅助,总结教程(有挂详情)1.反杀大厅辅助 ai辅助创建新账号,点...
玩家必备教程(微扑克操作)外挂... 玩家必备教程(微扑克操作)外挂透明挂辅助器(黑科技)软件(有挂详细)-哔哩哔哩是一款可以让一直输的玩...
解密关于!中至九江都昌讨赏辅助... 解密关于!中至九江都昌讨赏辅助,wepoker高级辅助,2025新版教程(有挂规律)运中至九江都昌讨...
如何分辨真伪(WePoKe私人... 如何分辨真伪(WePoKe私人局)外挂透明挂辅助下载(黑科技)真的(有挂秘诀)-哔哩哔哩是一款可以让...
分享实测!逗好乐游辅助器,po... 分享实测!逗好乐游辅助器,pokeplus脚本,分享教程(有挂秘籍)1、金币登录送、破产送、升级送、...
玩家必备攻略!指尖四川辅助脚本... 玩家必备攻略!指尖四川辅助脚本,wepoker买脚本靠谱,德州论坛(有人有挂)运指尖四川辅助脚本辅助...
今日头条(WePoKe记牌器)... 今日头条(WePoKe记牌器)外挂透明挂辅助下载(黑科技)ai软件功能介绍(真的有挂)-哔哩哔哩是一...
发现一款!新西部辅助是有挂意思... 发现一款!新西部辅助是有挂意思,wepoker钻石可以看底牌,透牌教程(有挂透视)在进入新西部辅助是...
一分钟揭秘(云扑克德州辅助器)... 一分钟揭秘(云扑克德州辅助器)外挂透明挂辅助器(黑科技)用ai有用(有挂透明挂)-哔哩哔哩;一分钟揭...