如何在MVC3中同时上传图像并将其与另一个类关联

发布于 2024-12-19 01:24:11 字数 755 浏览 4 评论 0原文

我有一门课程“活动”,我希望能够在活动页面上显示图像。我已经定义了图像类,但现在确定如何上传图像。我希望能够将图像存储在数据库中。

public class Event
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string  Description { get; set; }

        public string Address { get; set; }

        public string AddressTwo { get; set; }

        public virtual Person Owner { get; set; }

        public DateTime Date { get; set; }

        public virtual Image Image { get; set; }
    }

 public class Image 
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string AlternateText { get; set; }

        public virtual string CssClass { get; set; }

        public Byte[] File { get; set; }
    }

I have a class, Event, and i want to be able to have images on the event page. I have defined the image class but am now sure how i can upload the image. I want to be able to store the image in the database.

public class Event
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string  Description { get; set; }

        public string Address { get; set; }

        public string AddressTwo { get; set; }

        public virtual Person Owner { get; set; }

        public DateTime Date { get; set; }

        public virtual Image Image { get; set; }
    }

 public class Image 
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string AlternateText { get; set; }

        public virtual string CssClass { get; set; }

        public Byte[] File { get; set; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留蓝 2024-12-26 01:24:11

如果您想处理文件上传,您应该使用 HttpPostedFileBase 类型来表示图像而不是字节数组:

public class Image 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string AlternateText { get; set; }
    public virtual string CssClass { get; set; }
    public HttpPostedFileBase File { get; set; }
}

然后在您的视图中您将使用文件输入:

@model Event
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(x => x.Image.File)
        @Html.TextBox(x => x.Image.File, new { type = "file" })
    </div>
    ... some other fields
    <button type="submit">OK</button>
}

最后您将拥有控制器操作,表单将被发布到该操作并保存文件:

[HttpPost]
public ActionResult Upload(Event model)
{
    if (model.Image != null && model.Image.ContentLength > 0)
    {
        // an image was selected by the user => process and store it into the database
    }
    ...
}

您可能还会发现<一href="http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx" rel="nofollow">以下博客文章很有用。

If you want to handle file uploads you should use the HttpPostedFileBase type to represent the image and not a byte array:

public class Image 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string AlternateText { get; set; }
    public virtual string CssClass { get; set; }
    public HttpPostedFileBase File { get; set; }
}

then in your view you will use a file input:

@model Event
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.LabelFor(x => x.Image.File)
        @Html.TextBox(x => x.Image.File, new { type = "file" })
    </div>
    ... some other fields
    <button type="submit">OK</button>
}

and finally you will have the controller action to which the form will be posted and which will save the file:

[HttpPost]
public ActionResult Upload(Event model)
{
    if (model.Image != null && model.Image.ContentLength > 0)
    {
        // an image was selected by the user => process and store it into the database
    }
    ...
}

You might also find the following blog post useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文