【问题标题】:File input box upload image to database文件输入框上传图片到数据库
【发布时间】:2010-11-26 09:09:02
【问题描述】:

我的数据库中有一个 image 类型的字段,它在我的模型(ADO.NET 实体框架)中映射为二进制。

但是不知何故,我从输入文件框中得到的图像没有被传递给对象。我知道这一点是因为我调试了我的操作并且对象语言(我尝试上传到数据库的图像是一个标志)的属性标志设置为 null,这非常糟糕!它应该包含上传的图像。我还需要做点别的吗?

以下是我的表单 html 代码和我的操作代码:

<% using (Html.BeginForm("Create", "Language", FormMethod.Post, 
          new {enctype="multipart/form-data"})) {%>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Id">Id:</label>
            <%= Html.TextBox("Id") %>
            <%= Html.ValidationMessage("Id", "*") %>
        </p>
        <p>
            <label for="Name">Name:</label>
            <%= Html.TextBox("Name") %>
            <%= Html.ValidationMessage("Name", "*") %>
        </p>
        <p>
            <label for="Flag">Flag:</label>
            <!--
            File box is a helper that I got from this link:
            http://pupeno.com/blog/file-input-type-for-forms-in-for-asp-net-mvc/
            -->
            <%= Html.FileBox("Flag")  %>
            <%= Html.ValidationMessage("Flag", "*") %>
        </p>
        <p>
            <label for="IsDefault">IsDefault:</label>
            <%= Html.TextBox("IsDefault") %>
            <%= Html.ValidationMessage("IsDefault", "*") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

我正在使用 Visual Studio 2008。

[AcceptVerbs(HttpVerbs.Post)]  
public ActionResult Create(Language language)  
{  
    // Here language.Flag is null. Shouldn't the Flag property be a
    // binary field ready to be stored in the database along with the others?
    if (!ModelState.IsValid || !_service.CreateLanguage(language))  
    {  
        return View("Create", language);  
    }  
    return RedirectToAction("Index");  
}  

我做错了什么?

【问题讨论】:

    标签: database image upload


    【解决方案1】:

    我已经能够通过将 HttpPostedFileBase 参数添加到我的操作方法来上传文件。在这些情况下,我不相信 ASP.NET MVC 会自动为您填充语言参数的属性。

    【讨论】:

    • 那么,如何填充该属性?谢谢!
    • 它是为我自动填充的。我所要做的就是包含 HttpPostedFileBase 参数。在标记中,我没有使用 Html 助手(虽然这应该没问题)。我只是写出了
    【解决方案2】:

    文件上传的工作方式与普通帖子字段完全不同。您在 Request.Files 中有上传。我正在做这样的事情来把它捡起来

    if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0) {
        ModelState.AddModelError("Picture", "Picture is missing");
    } else if (!IsImage(Request.Files[0].ContentType)) {
        ModelState.AddModelError("Picture", "The picture must be a JPEG or PNG");
    } else {
        try {
            Save(Request.Files[0].InputStream);
        } catch {
            ModelState.AddModelError("Picture", "Something is wrong with the picture, we couldn't open it");
        }
    }
    

    IsImage 和 Save 在我的代码中实际上比这复杂得多。如果你想把它保存到模型和数据库中,你需要convert the stream into a byte array

    【讨论】:

      【解决方案3】:

      感谢您的回复,但幸运的是我找到了更好的方法。

      我更改了操作方法签名:
      [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(语言)

      到:
      [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(HttpPostedFileBase Flag, Language language)

      然后我将 Flag 文件名归因于 language.Flag 并仅保留对文件所在位置的引用(这是因为我们决定使数据库尽可能小,但我相信这个新签名也可以用于将文件转换为数据库)。

      希望这对其他人也有帮助!

      对不起,我不能给你竖起大拇指。声望不够……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-07
        • 2014-03-29
        • 2013-12-25
        • 2018-06-27
        • 2015-04-10
        相关资源
        最近更新 更多