我必须在UploadFile.cshtml表单
的单个提交中上传文件和其他数据我有一个基类,它是家庭控制器中mvc动作的输入。
我的基类,mvc动作方法和带有Razor脚本的cshtml部分在下面给出
我必须在提交UploadFile.cshtml表单时在mvc操作中将文件作为字节数组
我的基类
public class FileUpload
{
public string Name {get;set;}
public int Age {get;set;}
public byte[] Photo {get;set;}
}
MyMVCAction
[HttpPost]
public void UploadFile(FileUploadobj)
{
FileUploaddata=obj;
}
Mycshtmlform
@modelMVC.Models.FileUpload
@using(Html.BeginForm("UploadFile","Home",FormMethod.Post))
{
<fieldset>
<legend>File Upload</legend>
<div class="editor-label">
@Html.LabelFor(model=>model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model=>model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model=>model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model=>model.Age)
</div>
<div class="editor-label">
@Html.Label("UploadPhoto");
</div>
<div class="editor-field">
<input type="file" id="photo" name="photo"/>
</div>
<p>
<input type="submit" value="Create"/>
</p>
</fieldset>
}
答案 0 :(得分:1)
我认为更好地实现它:
[HttpPost]
public ActionResult UploadFile(FileUpload obj)
{
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
obj.Photo = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
//return some action result e.g. return new HttpStatusCodeResult(HttpStatusCode.OK);
}
我希望它有所帮助。