[英]File Upload ASP.NET MVC
Basically what I have at the moment is a ASP.NET MVC project made database first the tables take alpha numeric input but I think it would be pretty nifty to have a file upload so I can insert a picture into the database so I created a table in SQL server with an ID field and an image field with the data type image. 基本上我现在所拥有的是一个ASP.NET MVC项目,数据库首先使表格采用字母数字输入,但我认为上传文件会非常好,所以我可以将图片插入数据库,所以我创建了一个表格在具有ID字段的SQL服务器和具有数据类型图像的图像字段中。
When i add this model and create my controller and views based on that model it doesn't create the label or editor for this field, I used DataAnnotations to give it the DataType upload but I can't find a way of actually uploading the file I tried this which works for normal input but nothing shows up is there a way of having a @HTML.FileUploadFor(model => model.Vehicle) or something along those lines. 当我添加此模型并基于该模型创建我的控制器和视图时,它不会为此字段创建标签或编辑器,我使用DataAnnotations为其提供DataType上载但我找不到实际上载文件的方法我试过这个适用于普通输入,但没有任何显示是否有一种方法可以使用@ HTML.FileUploadFor(model => model.Vehicle)或类似的东西。
This is what I've tried 这就是我尝试过的
<div class="editor-label">
@Html.LabelFor(model => model.Vehicle)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Vehicle)
@Html.ValidationMessageFor(model => model.Vehicle)
</div>
This is my model 这是我的模特
public partial class VehicleImage
{
public int Vehicle_ID { get; set; }
[DataType(DataType.Upload)]
public byte[] Vehicle { get; set; }
}
There are many approaches to this. 有很多方法可以解决这个问题。
I usually use a jquery plugin called Uploadify. 我通常使用一个名为Uploadify的jquery插件。
Have a look at this post - https://stackoverflow.com/a/17520062/1581026 看看这篇文章 - https://stackoverflow.com/a/17520062/1581026
Another way is just by adding the following in your view inside your form: 另一种方法是在表单中的视图中添加以下内容:
<input type="file" name="Picture" />
Your form element needs to look like the following: 您的表单元素需要如下所示:
@using ( Html.BeginForm( "ACTION", "CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))
And then on your model that is getting passed to your POST Action you can add a attribute: 然后在您的模型上传递给您的POST操作,您可以添加一个属性:
public HttpPostedFileBase Picture {get; set; }
Or you can also just add it as a parameter in your POST Action: 或者您也可以在POST操作中将其添加为参数:
[HttpPost]
Public ActionResult SomeAction(HttpPostedFileBase Picture, .....)
There isn't a standard control or helper for files as far as I know. 据我所知,文件没有标准控件或助手。 However here is a nice tutorial to achieve what you want.
但是这里有一个很好的教程来实现你想要的。
http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/ http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
Furthermore, if you really want this functionality, you can write an html helper yourself. 此外,如果您真的想要这个功能,您可以自己编写一个html助手。
@helper MyHelper(string id){ <input type="file" id="@id" /> }
try something like this: 尝试这样的事情:
Create this type of view 创建此类视图
using (Html.BeginForm("FileUpload", "FileUpload",
FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input name="uploadFile" type="file" />
<input type="submit" value="Upload File" />
}
Here is controller method 这是控制器方法
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
}
Please refer this link : http://www.codeproject.com/Articles/38970/Implementing-HTTP-File-Upload-with-ASP-NET-MVC 请参考此链接: http : //www.codeproject.com/Articles/38970/Implementing-HTTP-File-Upload-with-ASP-NET-MVC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.