本文介绍了如何在Asp.net中添加图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cannot add image in about -> post method. This is my code please help.

这是我的控制器部分:

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" />`

我无法在特定部分上载图像.

I cannot upload the image in the specific section.

推荐答案

使用以下代码.

模型类:

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" />
    }

希望这一定会对您有所帮助.

Hope this will surely help you.

这篇关于如何在Asp.net中添加图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 03:53