将图像上传到文件夹mvc 5 Code First

时间:2014-12-22 15:00:11

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

我在最后一步中遇到了很长时间,将图像上传到文件夹。我没有保存数据库的路径。我现在想要这一切,现在将图像信息和图像路径保存到数据库,但图像本身不会出现在IMage文件夹中。我在Index和Create View之间跳转。提前感谢所有的帮助,对不起我的英语。 /

Create.cshtml

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()



    <h2>@ViewBag.Message</h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
    <p>
        @using (Html.BeginForm("Create", "Imagefiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            @Html.LabelFor(model => model.BildInfo, htmlAttributes: new { @class = "control-label col-md-2" })
            @Html.EditorFor(model => model.BildInfo, new { htmlAttributes = new { @class = "form-control" } })
            <label for="file">Upload Image:</label>
            <input type="file" name="file" id="file" />
            <input type="submit" value="Upload Image" />
        }
    </p>

}

INDEX.cshtml

@model IEnumerable<Upload.Models.Imagefiles>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.file)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BildInfo)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.file)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BildInfo)
        </td>
        <td><img src="~/Images/@item.file" width="100" height="100" /></td>


        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.FilID }) |
            @Html.ActionLink("Details", "Details", new { id=item.FilID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.FilID })
        </td>
    </tr>
}

</table>

IMAGEFILESCONTROLLER.CS

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "FilID,file,BildInfo")] Imagefiles imagefiles, HttpPostedFileBase imagesfiles)
        {
            if (ModelState.IsValid)
            {
                db.Imagefiles.Add(imagefiles);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var path = Path.Combine(Server.MapPath("~/Content/Files/"), imagesfiles.FileName);

            var data = new byte[imagesfiles.ContentLength];
            imagesfiles.InputStream.Read(data, 0, imagesfiles.ContentLength);

            using (var sw = new FileStream(path, FileMode.Create))
            {
                sw.Write(data, 0, data.Length);
            }

            return RedirectToAction("Index");

2 个答案:

答案 0 :(得分:0)

if (ModelState.IsValid)
        {
            db.Imagefiles.Add(imagefiles);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

我认为返回RedirectToAction(&#34;索引&#34;); 在上面的代码中,在它到达文件持久性逻辑之前退出执行。

行中,在if块中返回RedirectToAction(&#34;索引&#34;); 之前移动文件持久性逻辑。

你最终的代码会变成这样的东西。

 [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "FilID,file,BildInfo")] Imagefiles imagefiles, HttpPostedFileBase imagesfiles)
            {
                if (ModelState.IsValid)
                {
                    db.Imagefiles.Add(imagefiles);
                    db.SaveChanges();

                    var path = Path.Combine(Server.MapPath("~/Content/Files/"), imagesfiles.FileName);

                    var data = new byte[imagesfiles.ContentLength];
                    imagesfiles.InputStream.Read(data, 0, imagesfiles.ContentLength);

                    using (var sw = new FileStream(path, FileMode.Create))
                    {
                       sw.Write(data, 0, data.Length);
                     }

                    return RedirectToAction("Index");
                }
                else
                {
                  //Message to the user
                }
           }

答案 1 :(得分:0)

如果在行返回RedirectToAction(“索引”)之前仍未能在应用MrGenius分辨率后仍无法保存,请尝试以下代码。

使用imagesFile.SaveAs(path)而不是使用FileStream(.SaveAs更适合文件保存到磁盘)。代码imagesFile.SaveAs(路径)将物理文件保存到直接从HttpPostedFileBase指定的路径,然后你仍然保存到Database.Your FileStream类的路径sw.Write函数可能是这里的罪魁祸首。我尝试使用sw.Write写文件,没有保存。 FileStream可用于将图像文件保存到databases

//Direct file save to the path
var fileName = Path.GetFileName(imagesfiles.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Files/"), fileName);
imagesfiles.SaveAs(path);