繁体   English   中英

ASP.NET MVC 3文件从数据库下载的问题

[英]ASP.NET MVC 3 file download from database issue

编辑:

这是该表的创建脚本,减去简洁键的约束。

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。 下面是一些更重要的代码段的简化版本。

索引视图片段

@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; }

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

解决了这个问题,并且仍然使用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.

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

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

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

有关maxRequestLengthmaxAllowedContentLength的说明,请参见此问题

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM