ASP.Net MVC 2 图片上传
我正在尝试使用 MVC2 进行简单的图像上传。在我看来,我有:
<% using (Html.BeginForm("Upload","Home")) { %>
<input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
<input type="submit" value="Upload" />
<% } %>
在我的控制器(主页)中,如何上传此图像并将其保存到数据库?我对 ASP.Net MVC 非常陌生,这件事让我陷入困境。预先感谢您的帮助和时间。
编辑:
好吧,我想我的问题从我必须提供更多细节的答案来看是模糊的,这就是我所拥有的:
图像模型很简单,如下所示
public class ImageModel
{
public Image image;
public string ImageName;
public ImageModel(Image image, string name)
{
this.image = image;
ImageName = name;
}
}
-视图是这样的:
<%using (Html.BeginForm("Upload","Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{%>
<input type="text" id="ImageName" />
<input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
<input type="submit" value="Upload" />
<%} %>
控制器是我想要创建一个新的 ImageModel 实例,验证它,如果有效将其保存到数据库:所以我有:
public ActionResult Upload(ImageModel image)
{
//this is where i am stuck?
//how to get the supplied image as part of the ImageModel object
//whats the best way to retrieve the supplied image
ImageModel temp = image;
if(!temp.IsValid()){
//get errors
//return error view
}
uploadrepository.SaveImage(temp);
return View();
}
问题是如何获取提供的图像并将其保存到数据库
i am trying to do a simple image upload using MVC2. in my view i have:
<% using (Html.BeginForm("Upload","Home")) { %>
<input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
<input type="submit" value="Upload" />
<% } %>
In my controller (Home), how do i get this Image uploaded and save it to a database? I am very new to ASP.Net MVC and this thing has got me stuck. Thanks in advance for your help and time.
Edit:
okay, i guess my question is vague from the answer i got to provide more detail , This is what i have:
The image model is simple as below --
public class ImageModel
{
public Image image;
public string ImageName;
public ImageModel(Image image, string name)
{
this.image = image;
ImageName = name;
}
}
the view is like this:
<%using (Html.BeginForm("Upload","Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{%>
<input type="text" id="ImageName" />
<input type="file" name="upload" id="ImageUpload" value="Upload Image"/>
<input type="submit" value="Upload" />
<%} %>
the controller is where i want create a new ImageModel instance, validate it and if valid save it to the database: So i have:
public ActionResult Upload(ImageModel image)
{
//this is where i am stuck?
//how to get the supplied image as part of the ImageModel object
//whats the best way to retrieve the supplied image
ImageModel temp = image;
if(!temp.IsValid()){
//get errors
//return error view
}
uploadrepository.SaveImage(temp);
return View();
}
The question is how to get the supplied image and save it to the database
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的视图代码尝试将您的模型更改为此...
另外,您需要命名该文本输入元素(而不仅仅是 id)...
based on your View code try changing your model to this...
also, you'll need to name that text input element (not just id)...