@model Framely2011.Models.PictureUpload
@{
ViewBag.Title = "Upload";
}
<h2>Upload</h2>
@using (Html.BeginForm("Upload", "Member", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="myFile" id="myFile" />
@Html.TextBoxFor(x => x.MetaTagsObj.Meta1)<br />
@Html.TextBoxFor(x => x.MetaTagsObj.Meta2)<br />
@Html.TextBoxFor(x => x.MetaTagsObj.Meta3)<br />
<input type="submit" value="submit" />
}
这是我到目前为止,这是我的模型的样子:
public class PictureUpload
{
public HttpPostedFile fileName { get; set; }
public MetaTags MetaTagsObj { get; set; }
}
我不确定如何编写我的控制器以进行图片上传或如何在我执行[HttpPost]
答案 0 :(得分:3)
以下是我过去的做法(asp.net mvc 2)...使用.net 4和asp.net mvc 3可能更容易实现它。
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string mimeType = Request.Files[upload].ContentType;
Stream fileStream = Request.Files[upload].InputStream;
string fileName = Path.GetFileName(Request.Files[upload].FileName);
int fileLength = Request.Files[upload].ContentLength;
byte[] fileData = new byte[fileLength];
fileStream.Read(fileData, 0, fileLength);
//do something with the byte array (filedata)
}
HasFile()是一个定义如下的扩展方法:
public static class HttpPostedFileBaseExtensions
{
public static bool HasFile(this HttpPostedFileBase file)
{
return (file != null && file.ContentLength > 0);
}
}
enter code here
答案 1 :(得分:0)
您可以将模型作为[HttpPost]
操作方法中的参数,并阅读fileName
属性。