【问题标题】:how to add image in Asp.net?如何在 Asp.net 中添加图像?
【发布时间】:2020-03-04 01:59:21
【问题描述】:

我无法在 about -> post 方法中添加图像。这是我的代码,请帮忙。

这是我的控制器部分:

ActionResult Register(Admin adm)
{
    string fileName = Path.GetFileNameWithoutExtension(adm.ImageFile.FileName);
    string exe = Path.GetExtension(adm.ImageFile.FileName);
    fileName = fileName + DateTime.Now.ToString("yymmssfff") + exe;[enter image description here][1]
    adm.Adm_Image_path = "~/Image/" + fileName;
    fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
    adm.ImageFile.SaveAs(fileName);

    // Upload_Image(adm.ImageFile);

    if (ModelState.IsValid)
    {
        if (adm.Adm_Password == adm.Adm_Confirm_Password)
        {
            adm.Adm_Type = "Admin";
            db.admin.Add(adm);
            db.SaveChanges();

            return RedirectToAction("Login", "Home");
        }
    }

    return View();
}

在此处查看部分

<div class="form-group">
        @Html.LabelFor(x => x.Adm_Image_path, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="ImageFile" id="ImageFile" value="ImageFile" required />
            @Html.ValidationMessageFor(x => x.Adm_Image_path, "", new { @class="text-danger"})
        </div>
    </div>
    <input type="submit" value="Save" class="btn btn-default" />`

我无法上传特定部分的图片。

【问题讨论】:

  • 具体来说,adm.ImageFile.SaveAs(fileName); 不起作用?

标签: c# asp.net asp.net-mvc


【解决方案1】:

使用下面的代码。

模型类:

public class Admin
{
    public string ImagePath { get; set; }

    public HttpPostedFileBase ImageFile { get; set; }
}

控制器:

public ActionResult Register()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Register(Admin adm)
    {
        string fileName = Path.GetFileNameWithoutExtension(adm.ImageFile.FileName);
        string exe = Path.GetExtension(adm.ImageFile.FileName);
        fileName = fileName + DateTime.Now.ToString("yymmssfff") + exe;
        adm.ImagePath = "~/Images/" + fileName;
        fileName = Path.Combine(Server.MapPath("~/Images/"), fileName);
        adm.ImageFile.SaveAs(fileName);
        //Upload_Image(adm.ImageFile);

        if (ModelState.IsValid)
        {
            //if (adm.Adm_Password == adm.Adm_Confirm_Password)
            //{
            //    adm.Adm_Type = "Admin";
            //    db.admin.Add(adm);
            //    db.SaveChanges();
            //    return RedirectToAction("Login", "Home");
            //}
            return View();
        }
        else
        {
            return View();
        }
    }

查看:

@model testProject.Models.Admin
 @{
      ViewBag.Title = "Register";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }

    <h2>Register</h2>
    @using (Html.BeginForm("Register", "Signup", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      <div class="form-group">
        @Html.LabelFor(x => x.ImagePath, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
          <input type="file" name="ImageFile" id="ImageFile" value="ImageFile" required />
          @Html.ValidationMessageFor(x => x.ImagePath, "", new { @class = "text-danger" })
        </div>
      </div>
      <input type="submit" value="Save" class="btn btn-default" />
    }

希望这对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2021-07-21
    • 2012-07-12
    • 2012-04-25
    • 2012-04-17
    • 2016-09-11
    相关资源
    最近更新 更多