[英]ASP.NET MVC 3 file download from database issue
EDIT: 编辑:
Here's a create script for the table minus the constraints on the keys for brevity. 这是该表的创建脚本,减去简洁键的约束。
CREATE TABLE [dbo].[ProfileFile](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProfileID] [int] NOT NULL,
[FileTypeId] [int] NOT NULL,
[Data] [image] NOT NULL
EDIT: 编辑:
Attempt to bypass NHibernate to access data in service layer 尝试绕过NHibernate访问服务层中的数据
byte[] temp = (byte[])this._baseModelRepository.CurrentSession.CreateSQLQuery("SELECT Data FROM ProfileFile WHERE ProfileID = :ID").SetParameter("ID", id).UniqueResult();
EDIT: After checking binary data saved in db, it seems that the entire file is uploaded and that my issue is actually on the side of displaying the image. 编辑:检查保存在db中的二进制数据后,似乎整个文件已上传,而我的问题实际上在显示图像的一侧。
The image is not rendered but I can follow the action link to download the file but it is cut off at 8KB. 该图像未渲染,但我可以单击操作链接下载该文件,但该文件的大小为8KB。
ProfileFile Model class Data field as generated by NHibernate NHibernate生成的ProfileFile模型类数据字段
public virtual System.Byte[] Data { get; set; }
I hard coded the MIME type and file name for now while getting this set up. 在设置此代码时,我现在对MIME类型和文件名进行了硬编码。
public ActionResult GetProfilePhotoByID(int profileID)
{
var profileFile= //Load file object from DB by profileID
byte[] byteArray = profileFile.Data;
string mimeType = "image/jpg";
string fileName = "ProfilePhoto.JPG";
return File(byteArray, mimeType, fileName);
}
Using debugger I found that the size of profileFile.Data is 8000. Maybe this is a NHibernate restriction? 使用调试器,我发现profileFile.Data的大小为8000。也许这是NHibernate的限制?
Also,
this is running with IIS and
I am getting the same effect in Chrome, Firefox, and IE but am mainly testing in Chrome. 另外,
这是在IIS上运行的,
我在Chrome,Firefox和IE中获得相同的效果,但主要是在Chrome中进行测试。
Original question: I am trying to allow the user to upload a profile photo which I will then later serve up on their profile page. 原始问题:我试图允许用户上传个人资料照片,然后将其提供给他们的个人资料页面。 Unfortunately, the image file is not completely uploaded and is cut off at 8 KB.
不幸的是,该图像文件没有完全上传,并且被切断为8 KB。 Below are simplified versions of some of the more important code segments.
下面是一些更重要的代码段的简化版本。
Index View snippets 索引视图片段
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "profileform", enctype = "multipart/form-data" }))
<input type="file" id="photo" name="photo" />
Controller post action has HttpPostedFileBase photo
parameter 控制器发布操作具有
HttpPostedFileBase photo
参数
byte[] input;
if (photo != null && photo.ContentLength > 0)
{
input = new byte[photo.ContentLength];
photo.InputStream.Read(input, 0, photo.ContentLength);
if (model.ProfileFiles == null)
{
model.ProfileFiles = new List<Core.Model.ProfileFiles>();
}
model.ProfileFiles.Add(new Core.Model.ProfileFiles()
{
ProfileID = model.ID,
Profile = model,
Data = input
});
}
Model class is generated with NHibernate. 模型类是使用NHibernate生成的。 The relevant field is
相关领域是
// One To Many:
public virtual IList<ProfileFile> ProfileFiles { get; set; }
Just comment if more information is needed. 如果需要更多信息,请发表评论。
Solved the issue, and with using NHibernate still too, using Peter Gluck's answer to a related question. 解决了这个问题,并且仍然使用NHibernate,使用了Peter Gluck对相关问题的回答 。
var persistenceModel = AutoMap.AssemblyOf<Model.BaseModel>()
.Override<Model.ProfileFile>(map =>
{
// Sets max length of byte[] retrieved from image column of DB
map.Map(x => x.Data).Length(2147483647);
})
... etc.
Try setting the following in your Web.config
尝试在
Web.config
设置以下内容
<system.web>
<!--50MB-->
<httpRuntime maxRequestLength="51200"/>
</system.web>
and 和
<system.webServer>
<security>
<requestFiltering>
<!--50MB-->
<requestLimits maxAllowedContentLength="52428795" />
</requestFiltering>
</security>
</system.webServer>
For an explanation of maxRequestLength
and maxAllowedContentLength
see this question . 有关
maxRequestLength
和maxAllowedContentLength
的说明,请参见此问题 。
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input id="file" type="file" name="file" />
<input type="submit" value="Upload" class="btn" />
}
public ActionResult ChangePhoto(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
using (System.Drawing.Image Img = System.Drawing.Image.FromStream(file.InputStream))
{
Img.Save(Server.MapPath("~") +"/myimage.png", Img.RawFormat);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.