2013-07-23 51 views
1

我正在建立一个简单的图像库,使用ASP.NET MVC 4.什么是创建方法从用户PC获取图像的最佳方式是什么?我的意思是有没有办法在浏览器中打开文件资源管理器,然后将选定的文件复制到/ Content/Images文件夹?添加图像dynamicaly asp net mvc 4

回答

1

创建方法从用户PC获取图像的最佳方式是什么?

使用文件输入控制,并让您的行动采取文件:

@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="image" /> 
    <input type="submit" value="Upload" /> 
} 

那么你的职位的行动将是:

public ActionResult YourAction(HttpPostedFileBase image) 
{ 
    //do whatever with the image 
} 

我的意思是有没有办法打开浏览器中的文件浏览器,然后将选定的文件复制到/ Content/Images文件夹?

不,进入Content/Images文件夹。您应该考虑托管解决方案/数据库来存储这些图像。

+0

谢谢,正是我在找的东西。但可以解释为什么Content foled是个坏主意? msdn教程中的人使用它。 –

+0

@AlexDefine Content文件夹并不是一个坏主意(这是标准惯例,它带有MVC项目模板),动态添加内容但不是最好的主意。 – mattytommo

+0

我明白了。谢谢 :) –