如何在mvc 3中上传图片 [英] how to upload image in mvc 3

查看:61
本文介绍了如何在mvc 3中上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试上传mvc3中的图片。

但是当我提交表单模型属性文件时没有获取文件信息

iam使用输入标记如下



@ Html.TextBoxFor(model => model.File,new {type =file})



如何解决上述问题

请任何人帮帮我



先谢谢

解决方案

确保表单上的enctype设置为multipart / form-data



什么数据类型是model.File?



检查出来:使用ASP.NET MVC实现HTTP文件上载 [ ^ ]



我正在编辑这个,所以我可以显示代码。如果你需要特定的帮助,你需要发布你的代码,但我刚刚提出了一个适合我的快速解决方案,这是我的代码:



模型:

  public   class  MyModel 
{
public HttpPostedFileBase MyFile { get ; set ; }
}





视图:

 @model FileUploadTest.Models.MyModel 

@ {
Layout = null ;
}

<!DOCTYPE html>

< html>
< head>
< title> title < / title >
< / head >
< body>
< div>
@using(Html.BeginForm( PostFile Home,FormMethod.Post, new {enctype = multipart / form-data}))
{
< div>
上传文件:@ Html.TextBoxFor(m = > m.MyFile, new { Type = file})
< p>
< input type = submit = go />
< / p >
< / div >
}
< / div >
< / body >
< / html < span class =code-keyword>>





控制器:

< pre lang =c#> public ActionResult PostFile(MyModel myModel)
{
return this .View();
}





注意:我在控制器中没有做任何事情,只设置一个断点,并确保该值被回发,它是。我在这里包含了控制器动作,所以你可以确保你把你的模型传回去了。



如需进一步的帮助你真的需要发布你自己的代码,这样我们就能看到你在做什么。


i am trying to upload image in mvc3.
but when i submit the form model property file is not getting the file information
iam using input tag as follows

@Html.TextBoxFor(model=>model.File,new {type="file"})

how to solve above problem
please any body help me

Thanks in Advance

解决方案

Make sure you have the enctype on the form set to multipart/form-data

What data type is model.File ?

Check this out : Implementing HTTP File Upload with ASP.NET MVC[^]

I'm editing this so I can show code. You will need to post your code if you want specific help, but I just whipped up a quick solution that worked for me, here's my code:

The Model:

public class MyModel
   {
       public HttpPostedFileBase MyFile { get; set; }
   }



The View:

@model FileUploadTest.Models.MyModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
    <head>
        <title>title</title>
    </head>
    <body>
        <div>
              @using (Html.BeginForm("PostFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
              {
                  <div>
                      Upload file: @Html.TextBoxFor(m => m.MyFile, new { Type="file"})
                      <p>
                          <input type="submit" value="go"/>
                      </p>
                  </div>
              }
        </div>
    </body>
</html>



The controller:

public ActionResult PostFile(MyModel myModel)
       {
           return this.View();
       }



NOTE: I didn't do anything in the controlller, only set a breakpoint and made sure the value was being posted back and it was. I included the controller action here so you could make sure you were passing your model into it with the post back.

For further help you will really need to post your own code so we can see what you're doing.


这篇关于如何在mvc 3中上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆