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

相关内容

热门资讯

更值得关注的是!wepoker... 更值得关注的是!wepoker到底有没有透视,德扑之星辅助器怎么用,竟然确实有挂(有挂教学)1、完成...
截至目前!德州透视是真的假的,... 截至目前!德州透视是真的假的,德普之星怎么开辅助,果然真的有挂(有挂功能)1、德普之星怎么开辅助脚本...
明白辅助挂!wepoker辅助... 明白辅助挂!wepoker辅助器安装包定制,德普软件,果然是有挂(的确有挂)1、德普软件脚本辅助下载...
黑科技辅助!werplan辅助... 黑科技辅助!werplan辅助软件,德普之星透视辅助软件是真的吗,确实是有挂(有挂方略)德普之星透视...
做出回应!pokemmo免费脚... 做出回应!pokemmo免费脚本,德普辅助器怎么用,真是是真的有挂(有挂方略)暗藏猫腻,小编详细说明...
目前!wpk软件是正规的吗,智... 目前!wpk软件是正规的吗,智星菠萝透视,真是是有挂(有挂方略)1、下载好智星菠萝透视脚本下载之后点...
事发当天!hardrock作弊... 事发当天!hardrock作弊,德扑之星有辅助软件吗,竟然是有挂(有挂透视)1、德扑之星有辅助软件吗...
随着!wepoker可以开透视... 随着!wepoker可以开透视吗,智星德州插件怎么下载,好像真的是有挂(有挂教学)一、智星德州插件怎...
做出回应!如何下载wpk透视版... 做出回应!如何下载wpk透视版,德扑圈有透视吗,原来是真的有挂(存在有挂)1、起透看视 德扑圈有透视...
黑科技攻略!poker wor... 黑科技攻略!poker world辅助器,德普之星透视辅助软件激活码,原来是真的有挂(有挂详细)1、...