在MVC中上传图片时如何解决此错误?

时间:2019-09-23 09:30:29

标签: asp.net-mvc model-view-controller

我是否正在使用带有SQL Server Management Studio的MVC?

错误:未为此对象定义无参数构造函数

我不知道是什么错误?

我在3个地方换了

1.Index.cshtml

2.create.cshtml

3.CrudManuallyController.cs

公共字符串Image {get;组; } varchar(50)

CrudManually Controller:   
    public class CrudManuallyController : Controller
    {
            public ActionResult Create()
            {
                return View();
            }

            // POST: CrudManually/Create
            [HttpPost]
            public ActionResult Create(manage manages,HttpPostedFileBase image)
            {
                try
                {
                      var folderPath = Server.MapPath("~/Images/");
                      image.SaveAs(Path.Combine("~/Images/", image.FileName));
                      manages.Image = Path.Combine("~/Images/", image.FileName);

                      // TODO: Add insert logic here
                      db.manages.Add(manages);
                      db.SaveChanges();
                      return RedirectToAction("Index");
                }
                catch
                {
                    return View(manages);
                }
            }
    }
create.cshtml
    <h2>Create</h2>
    @using (Html.BeginForm("Create", "CrudManually", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
                <div class="editor-label">
                    @Html.LabelFor(model => model.Image)
                </div>
             <div class="editor-field">
                <input id="Image" title="Image Uploading" type="file" name="image" />
            </div>
    }
Index.cshtml
<table>
          <tr>
            <th>
                @Html.DisplayNameFor(model => model.Image)
            </th>
          </tr>
          <tr>
            @foreach (var item in Model)
            {
               <td>
                 <img src="@Url.Content(item.Image)" alt="Image not display" width="20%" height="20%" />
                </td>
            }
          </tr>
</table>        

图片: enter image description here

如何解决此错误? 我不知道这是什么错误? 任何代码都会添加到我的程序中,并成功运行我的图片上传,该怎么办?

图片1:

enter image description here

2 个答案:

答案 0 :(得分:1)

CrudManual Controller

//replace HttpPostedFile with HttpPostedFileBase object
//please add server.Mappath function into code for exact foler structure to save file
public class CrudManuallyController : Controller
{
        public ActionResult Create()
        {
            return View();
        }

        // POST: CrudManually/Create
        [HttpPost]
        public ActionResult Create(manage manages,HttpPostedFileBase image)
        {
            try
            {
                var folderPath = Server.MapPath("~/Images/");
                image.SaveAs(Path.Combine(folderPath, image.FileName));
                manages.Image= Path.Combine(folderPath, image.FileName);
                // TODO: Add insert logic here
                db.manages.Add(manages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(manages);
            }
        }
}

create.cshtml

//encytype is wrong please replace it with enctype
//input file name="file" so it will not set on controller side as its name on controller side is 'image', so you need to repalce name='file' with name='image'

<h2>Create</h2>
@using (Html.BeginForm("Create", "CrudManually", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
            <div class="editor-label">
                @Html.LabelFor(model => model.Image)
            </div>
            <div class="editor-field">
                <input id="Image" title="Image Uploading" type="file" name="image" />
            </div>
}

Index.cshtml

<table>
          <tr>
            <th>
                @Html.DisplayNameFor(model => model.Image)
            </th>
          </tr>
          <tr>
            @foreach (var item in Model)
            {
               <td>
                   <img src="@Url.Content('~/Images/' + item.Image)" alt="Image not display" width="20%" height="20%" />
                </td>
            }
          </tr>
</table>      

说明:

ASP.NET C#MVC提供HttpPostedFileBase类的功能,该类是一个抽象类,它包含与HttpPostedFile类相同的成员。因此我们可以在将文件上传到服务器上时使用此抽象类。

第1步:为此,我们需要使用具有 name属性的文件的输入类型。

第2步:表单必须是具有 enctype =“ multipart / form-data”

的POST

第3步:在控制器端,我们需要获取与我们已经为输入文件类型指定的名称相同的HttpPostedFileBase对象值

public ActionResult Create(manage manages,HttpPostedFileBase image)

第4步:按照Form post的所有步骤操作后,您将在HttpPostedFileBase类型的图像对象中获取file的值,然后需要检查可为空的条件,并简单地对文件进行代码保存。

var virtualPath = StaticValues.AdvertisementImagePath;
var physicalPath = Server.MapPath(virtualPath);
Utilities.SaveFile(fileObject, virtualPath, physicalPath, "FILE PATH");

答案 1 :(得分:1)

控制器代码

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

[HttpPost]
public ActionResult Create(manages manages, HttpPostedFileBase image)
{
    try
    {
        if (image != null)
        {
            //using System.IO;
            var folderPath = Server.MapPath("~/Images/");
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            //Path.Combine for concate folder path and image name together
            var imagePathName = Path.Combine(folderPath, image.FileName);
            image.SaveAs(imagePathName);
            manages.Image = image.FileName;
            //TODO: Add insert logic here
            //db.manages.Add(manages);
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }
    catch
    {
        return View(manages);
    }
}

create.cshtml代码

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="editor-label">
        Image
    </div>
    <div class="editor-field">
        <input id="Image" title="Image Uploading" type="file" name="image" />
    </div>
    <input type="submit" value="button" />
}

Index.cshtml

<table>
          <tr>
            <th>
                @Html.DisplayNameFor(model => model.Image)
            </th>
          </tr>
          <tr>
            @foreach (var item in Model)
            {
               <td>
                 <img src="@Url.Content('~/Images/' + item.Image)" alt="Image not display" width="20%" height="20%" />
                </td>
            }
          </tr>
</table>