ASP.Net MVC 2 图片上传

发布于 2024-10-14 12:04:35 字数 1575 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

倥絔 2024-10-21 12:04:35

根据您的视图代码尝试将您的模型更改为此...

public class ImageModel
{
    public HttpPostedFileWrapper upload { get; set; }
    public string ImageName { get; set; } 
}

另外,您需要命名该文本输入元素(而不仅仅是 id)...

<input type="text" id="ImageName" name="ImageName" />

based on your View code try changing your model to this...

public class ImageModel
{
    public HttpPostedFileWrapper upload { get; set; }
    public string ImageName { get; set; } 
}

also, you'll need to name that text input element (not just id)...

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