我有一个视图模型,有一个名为'StudentImage'的HttpPostedFileBase属性。当用户登录时,我想要获取一个字节数组(我的图像从数据库)并显示它?我可以从数据库中获取byte [],并且可以通过设置从httppostedfilebase继承的内存流,将byte []设置回我的HttpPostedFileBase Image。但是,没有图像显示出来的窗体上HttpPostedFileBase不显示图像与引导文件上传插件
这是我的视图模型
public class EditStudentViewModel
{
...other code here
public HttpPostedFileBase StudentImage { get; set; }
}
这是我的控制器,其中我取字节数组,我想的字节设置[]为“StudentImage”,这是一个HttpPostedFileBase
public ActionResult Edit()
{
var years = Enumerable.Range(DateTime.Now.Year - 100, 100).Reverse();
string userId = User.Identity.GetUserId();
Student student = studentRepository.Find(userId);
// student.StudentImage => this is a byte array that I need to get
// into the HttpPostedFileBase named StudentImage into
// 'EditStudentViewModel'
var studentViewModel = new EditStudentViewModel
{
...other properties set here
StudentImage = new MemoryFile(new MemoryStream(student.StudentImage.Image));
};
我创建了一个名为MemoryFile新类和继承HttpPostedBaseFIle像这样
class MemoryFile : HttpPostedFileBase
{
Stream stream;
public MemoryFile(Stream stream)
{
this.stream = stream;
}
public override Stream InputStream
{
get { return stream; }
}
}
它似乎正确设置值,但是当我在屏幕上查看窗体时,我看不到图像!它不与引导文件插件设置我使用的可以在这里找到BootStrap File Upload Plugin
这里是我的文件uplaod插件的JavaScript
$("#input-20").fileinput({
'type': 'POST',
'cache': false,
'browseClass': 'btn btn-primary btn-block',
'showCaption': false,
'showRemove': false,
'showUpload': false,
'uploadAsync': false,
'maxFileCount': 1,
'allowedFileExtensions': ['jpg', 'png', 'gif'],
'allowedFileTypes': ['image'],
//'uploadUrl': '@Url.Action("Edit", "Student")'
});
这是我的HTML标签
<div class="panel-body">
@Html.TextBoxFor(model => model.StudentImage, new { @type = "file", @id = "input-20" })
</div>
你不想返回实际的文件,你想返回一个路径到图像文件。 – DLeh 2015-02-11 21:50:30
我没有将图像存储在本地存储上,我将它存储在我的数据库中的varbinary(max)数据类型中,该数据类型返回为一个字节[] – user1186050 2015-02-11 21:55:02
http://stackoverflow.com/questions/880515/display-图像从数据库在asp-mvc – DLeh 2015-02-11 21:55:51