【问题标题】:How to insert image in website (Asp.net mvc 5)如何在网站中插入图像(Asp.net mvc 5)
【发布时间】:2020-06-16 23:41:24
【问题描述】:

我想让用户从他们的电脑中选择一张图片并将其上传到我的网站,但是它没有打开浏览选项来选择一张图片并返回到列表视图。

这是我的代码:

[HttpPost]
        public ActionResult AddRestuarantItem(RestaurantItem restaurantItem, HttpPostedFileBase certificateImage)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (certificateImage != null)
                    {
                        string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
                            Path.GetFileName(certificateImage.FileName));

                        certificateImage.SaveAs(path);
                    }

                    ViewBag.FileStatus = "Image uploaded successfully";
                }
                catch (Exception)
                {
                    ViewBag.FileStatus = "Error while image uploading.";
                }
            }

            restaurantItems.Insert(restaurantItem);
            restaurantItems.Commit();

            return RedirectToAction("RestaurantItemList");
        }

这是我的观点:

@model WorldOfHalal.Models.RestaurantItem

@{
    ViewBag.Title = "AddRestuarantItem";
}

<h2>AddRestuarantItem</h2>


@using (Html.BeginForm("AddRestuarantItem", "Restaurant", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>RestaurantItem</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ItemName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ItemName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CertificateImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CertificateImageUrl, new { htmlAttributes = new { @class = "form-control", @type = "CertificateImageUrl" } })
                @Html.ValidationMessageFor(model => model.CertificateImageUrl, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="image" value="Upload" class="btn btn-primary" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10 text-success">
                @ViewBag.FileStatus
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

【问题讨论】:

    标签: html asp.net-mvc-5


    【解决方案1】:

    你必须输入 type="file" 来上传图片,像这样:

     <input type="file" name="certificateImage"/>
    

    【讨论】:

    • 它没有在数据库中插入图像:S
    • 我们将图像存储在服务器上的文件夹中。您只需要将图像名称存储在数据库中即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2018-01-29
    • 2020-06-26
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多