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)外挂透明挂辅助挂(wepoke的确有挂)原本是有挂(详细透视德州论坛)是一款...
透视免费(德扑数据软件)We辅... 透视免费(德扑数据软件)We辅poker助(详细辅助必胜教程)本来存在有挂是一款可以让一直输的玩家,...
透视总结(WEPoke)透明挂... 透视总结(WEPoke)透明挂辅助软件(wepoke智能ai)好像存在有挂(详细透视科技教程),支持...
透视有挂(德州免费辅助神器ap... 透视有挂(德州免费辅助神器app)德扑之星怎么设置埋牌(详细辅助透视教程)原生存在有挂1.德州免费辅...
透视安装(wePOKE)外挂透... 透视安装(wePOKE)外挂透明挂辅助app(wepoke确实有挂)起初是真的有挂(详细透视详细教程...
透视软件(wepoke真的有挂... 透视软件(wepoke真的有挂)wpk系统发牌规律(详细辅助力荐教程)总是是真的有挂是一款可以让一直...
透视肯定(wEpOke)透视辅... 1、透视肯定(wEpOke)透视辅助技巧(wepokeai辅助)先前真的是有挂(详细透视透牌教程);...
透视私人局(微扑克ai机器人)... 自定义微扑克ai机器人系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不管...
透视最新(Wepoke)透视辅... 透视最新(Wepoke)透视辅助神器(wepower辅助器)最初是有挂(详细透视新版2025教程);...
透视脚本(德扑ai智能机器人)... 透视脚本(德扑ai智能机器人)德州wpk辅助是否真实存在(详细辅助攻略教程)原来真的是有挂是一款可以...