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);
}

相关内容

热门资讯

透视苹果版(德扑数据软件)德州... 透视苹果版(德扑数据软件)德州辅助软件线上(详细辅助安装教程)原先是真的有挂1.德扑数据软件 ai辅...
透视新版(wepOKE)透明挂... 透视新版(wepOKE)透明挂辅助安装(WePoKe透明挂)原来有挂(详细透视切实教程);支持多人共...
透视好友(wePokE)外挂透... 透视好友(wePokE)外挂透明挂辅助app(wepoke模拟器)好像真的是有挂(详细透视必赢方法)...
透视神器(aapoker辅助工... 透视神器(aapoker辅助工具)德扑牌型胜率(详细辅助高科技教程)好像存在有挂1.aapoker辅...
透视了解(wePoke)透视辅... 透视了解(wePoke)透视辅助神器(wepoke辅助)一向是真的有挂(详细透视透明教程)这是由厦门...
透视ai(wpk透视辅助测试)... 透视ai(wpk透视辅助测试)wepoke辅助(详细辅助辅助教程)往昔是有挂您好,wpk透视辅助测试...
透视脚本(wepOkE)透视辅... 1、透视脚本(wepOkE)透视辅助挂(WePoKe透视挂)其实是有挂(详细透视细节方法)2、进入游...
透视脚本(德州之星有辅助挂)p... 透视脚本(德州之星有辅助挂)pokermaster有外挂吗(详细辅助技巧教程)原先是有挂1.德州之星...
透视app(wEpoke)透视... 透视app(wEpoke)透视辅助挂(WePoKe外挂)总是有挂(详细透视德州教程);玩家必备必赢加...
透视智能ai(wepoke有辅... 透视智能ai(wepoke有辅助挂)红龙扑克发牌有问题(详细辅助wpk教程)好像存在有挂1、这是跨平...