C# HttpPostedFileBase未显示带有引导文件上载插件的图像
我有一个视图模型,它有一个名为“StudentImage”的HttpPostedFileBase属性。当用户登录时,我想从数据库中获取一个字节数组并显示我的图像?我可以从数据库获取字节[],通过设置从HttpPostedFileBase继承的内存流,我可以将字节[]设置回HttpPostedFileBase映像。但我的表格上没有显示图像 这是我的视图模型C# HttpPostedFileBase未显示带有引导文件上载插件的图像,c#,asp.net-mvc,twitter-bootstrap,bootstrap-file-upload,C#,Asp.net Mvc,Twitter Bootstrap,Bootstrap File Upload,我有一个视图模型,它有一个名为“StudentImage”的HttpPostedFileBase属性。当用户登录时,我想从数据库中获取一个字节数组并显示我的图像?我可以从数据库获取字节[],通过设置从HttpPostedFileBase继承的内存流,我可以将字节[]设置回HttpPostedFileBase映像。但我的表格上没有显示图像 这是我的视图模型 public class EditStudentViewModel { ...other code here public
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; }
}
}
它似乎正确地设置了值,但当我在屏幕上查看表单时,我看不到图像!它没有设置我使用的引导文件插件,可以在这里找到
这是我的文件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>
不能在中显示图像,必须使用标记。当您执行@Html.TextBoxForx=>x,new{type=file}时,呈现的输出将是一个,没有什么特别的 如果需要显示现有图像,应执行以下操作:
您不希望返回实际文件,而是希望返回图像文件的路径。我没有将图像存储在本地存储器中,而是将其存储在数据库中的varbinarymax数据类型中,该数据类型以字节[]的形式返回。Hi-Dleh,我查看了您的链接,但大多数响应显示了用于显示数据的img标记。我的标签不是img标签,它是一个连接到文本框的引导插件。如果你想在网页上显示图像,你需要一个图像标签。
<div class="panel-body">
<!-- show the current StudentImage in the database -->
<img src="@Url.Action("GetStudentImage", new { studentID= Model.StudentImageID })" />
<!-- for adding a new StudentImage -->
@Html.TextBoxFor(model => model.StudentImage, new { @type = "file", @id = "input-20" })
</div>
public ActionResult GetStudentImage(int studentImageID)
{
Student student = studentRepository.Find(studentID);
return File(student.StudentImage.Image, "image/jpg");
}