【问题标题】:ASP.NET MVC 3 file download from database issue从数据库问题下载 ASP.NET MVC 3 文件
【发布时间】:2012-06-25 18:29:20
【问题描述】:

编辑:

为简洁起见,这是一个表的创建脚本减去键的约束。

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。也许这是NHibernate的限制?

另外,这是在 IIS 上运行的,我在 Chrome、Firefox 和 IE 中得到了相同的效果,但主要是在 Chrome 中进行测试。

原问题: 我正在尝试允许用户上传个人资料照片,然后我将在他们的个人资料页面上提供该照片。不幸的是,图像文件没有完全上传,被截断为 8 KB。下面是一些更重要的代码段的简化版本。

索引视图 sn-ps

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "profileform", enctype = "multipart/form-data" }))

<input type="file" id="photo" name="photo" />

控制器发布动作有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
                });
            }

模型类是用 NHibernate 生成的。相关字段是

// One To Many:
public virtual IList<ProfileFile> ProfileFiles { get; set; }

如果需要更多信息,请发表评论。

【问题讨论】:

  • 你用的是什么浏览器?那是一个内部网应用程序吗?你在 IIS 上运行它吗?
  • 您是否尝试上传一个小文件(例如 10 kb)?
  • 你能看到数据库是如何存储大文件的吗?不像 0x0000000,不是吗?
  • 我尝试了一个 6KB 的文件,整个过程都通过了。我认为在读取模型属性时限制已设置。我在 SQL Server 中使用图像列,它看起来像格式 0x0000000。
  • 好的。 0x0000 为空。也许这会有所帮助:stackoverflow.com/questions/10955051/…

标签: c# sql-server asp.net-mvc-3 nhibernate razor


【解决方案1】:

解决了这个问题,并且仍然使用 NHibernate,使用 Peter Gluck's answer 解决相关问题。

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.

【讨论】:

  • 仅供参考,这一直在生产中的 IIS 中工作。也可以告诉它它是可以为空的。 map.Map(x =&gt; x.Data).CustomType("BinaryBlob").Length(1048576).Nullable();
【解决方案2】:

尝试在您的Web.config 中设置以下内容

<system.web>
    <!--50MB-->
    <httpRuntime maxRequestLength="51200"/>
</system.web>

<system.webServer>
    <security>
        <requestFiltering>
            <!--50MB-->
            <requestLimits maxAllowedContentLength="52428795" />
        </requestFiltering>
    </security>
</system.webServer>

有关maxRequestLengthmaxAllowedContentLength 的说明,请参阅this question

【讨论】:

  • 没有解决问题。另外,这两个值都代表 50MB 吗?你能解释一下他们的单位吗?
  • 是的,它们都代表 50MB。老实说,我不知道他们为什么选择不同的单位。也许其他人可以向我们解释原因。一种设置是 ASP.NET 设置,另一种是 IIS 设置。 maxRequestLength 以 KB 为单位,而 maxAllowedContentLength 以字节为单位
【解决方案3】:
@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);
      }
 }

【讨论】:

  • 我不仅尝试保存图像,而且还特别尝试使用 NHibernate 模型绑定从数据库中存储和检索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多