如何在MVC3中同时上传图像并将其与另一个类关联
我有一门课程“活动”,我希望能够在活动页面上显示图像。我已经定义了图像类,但现在确定如何上传图像。我希望能够将图像存储在数据库中。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想处理文件上传,您应该使用 HttpPostedFileBase 类型来表示图像而不是字节数组:
然后在您的视图中您将使用文件输入:
最后您将拥有控制器操作,表单将被发布到该操作并保存文件:
您可能还会发现<一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:
then in your view you will use a file input:
and finally you will have the controller action to which the form will be posted and which will save the file:
You might also find the following blog post useful.