C# ASP.NET MVC 3文件从数据库下载问题
编辑: 下面是一个表的创建脚本,为了简洁起见,它减去了键上的约束C# ASP.NET MVC 3文件从数据库下载问题,c#,sql-server,asp.net-mvc-3,nhibernate,razor,C#,Sql Server,Asp.net Mvc 3,Nhibernate,Razor,编辑: 下面是一个表的创建脚本,为了简洁起见,它减去了键上的约束 CREATE TABLE [dbo].[ProfileFile]( [ID] [int] IDENTITY(1,1) NOT NULL, [ProfileID] [int] NOT NULL, [FileTypeId] [int] NOT NULL, [Data] [image] NOT NULL 编辑: 试图绕过NHibernate访问服务层中的数据 byte[] temp = (byte[])this._baseModelR
CREATE TABLE [dbo].[ProfileFile](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProfileID] [int] NOT NULL,
[FileTypeId] [int] NOT NULL,
[Data] [image] NOT NULL
编辑:
试图绕过NHibernate访问服务层中的数据
byte[] temp = (byte[])this._baseModelRepository.CurrentSession.CreateSQLQuery("SELECT Data FROM ProfileFile WHERE ProfileID = :ID").SetParameter("ID", id).UniqueResult();
编辑:
在检查了保存在db中的二进制数据之后,似乎整个文件都被上传了,而我的问题实际上是在显示图像方面
图像没有渲染,但我可以按照操作链接下载文件,但它在8KB处被切断
NHibernate生成的ProfileFile模型类数据字段
public virtual System.Byte[] Data { get; set; }
在进行设置时,我现在硬编码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);
}
使用调试器,我发现profileFile.Data的大小是8000。也许这是一个非纤维限制
此外,这是在IIS上运行的,我在Chrome、Firefox和IE上也得到了同样的效果,但我主要在Chrome上进行测试
原始问题:
我正在尝试允许用户上传个人资料照片,然后我会在他们的个人资料页面上提供。不幸的是,图像文件没有完全上传,并且在8KB时被切断。下面是一些更重要代码段的简化版本
索引视图片段
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "profileform", enctype = "multipart/form-data" }))
<input type="file" id="photo" name="photo" />
模型类由NHibernate生成。相关字段为
// One To Many:
public virtual IList<ProfileFile> ProfileFiles { get; set; }
//一对多:
公共虚拟IList配置文件{get;set;}
如果需要更多信息,请发表评论。尝试在
Web.config中设置以下内容
<system.web>
<!--50MB-->
<httpRuntime maxRequestLength="51200"/>
</system.web>
及
有关maxRequestLength
和maxAllowedContentLength
的说明,请参阅。@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/form data”}))
{
}
公共操作结果变更照片(HttpPostedFileBase文件)
{
如果(file!=null&&file.ContentLength>0)
{
使用(System.Drawing.Image Img=System.Drawing.Image.FromStream(file.InputStream))
{
保存(Server.MapPath(“~”+”/myimage.png),Img.RawFormat);
}
}
解决了这个问题,使用NHibernate也解决了相关问题
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.
var persistenceModel=AutoMap.AssemblyOf()
.Override(映射=>
{
//设置从DB的图像列检索的字节[]的最大长度
map.map(x=>x.Data).Length(2147483647);
})
等
没有解决这个问题。另外,这两个值都代表50MB吗?你能解释一下它们的单位吗?是的,它们都代表50MB。老实说,我不知道它们为什么选择不同的单位。也许其他人可以告诉我们为什么。一个设置是ASP.NET设置,另一个是IIS设置。maxRequestLength以KB为单位,而maxAllowedCon以KB为单位tentLength以字节为单位。您使用的浏览器是什么?这是内部网应用程序吗?您是否使用IIS运行它?您是否尝试上载一个小文件(例如10 kb)?你能看到数据库是如何存储大文件的吗?它们不像0x0000000,不是吗?我尝试了一个6KB的文件,整个过程都通过了。我认为在读取模型属性时,限制是不稳定的。我在SQL Server中使用了一个图像列,看起来格式是0x0000000.Ok。0x0000为空。也许这会有帮助:我不仅仅是trying以保存图像,但特别是尝试使用NHibernate model binding.FYI从数据库存储和检索图像,这在IIS的生产环境中一直有效。还可以告诉它它可以为null。map.map(x=>x.Data)。CustomType(“BinaryBlob”).Length(1048576)。null();
<system.webServer>
<security>
<requestFiltering>
<!--50MB-->
<requestLimits maxAllowedContentLength="52428795" />
</requestFiltering>
</security>
</system.webServer>
@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);
}
}
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.