文件上传和保存到数据库不正确

发布于 2024-10-01 09:12:05 字数 2685 浏览 3 评论 0原文

我在将文件上传到数据库时遇到一些问题。我上传到的表具有以下结构:

dbo.Cover
CoverID           int PK
CoverFileContent  varbinary(max)
CoverMimeType     nvarchar(50)
CoverFileName     nvarchar(50)
FileID            int FK

我可以使用 MVC 应用程序上传文件,不会出现任何错误,但是在数据库中,文件在 CoverFileContent 中存储为“0x0000000000000000......”。与文件相关的所有其他信息(例如 MimeType、FileName、CoverID 等)均正确上传。我疯狂猜测这是不正确的,所以我下载了该文件(创建了一个 .net MVC 下载器)。它似乎下载为正确类型的文件,但是当我尝试打开它时,它告诉我无法打开它。

这是我遵循的原始教程: 教程。我已经让它完美地工作了,但是我想使用 ADO.net,所以我稍微重写了一下。我没有做出任何重大更改,但是从我的代码可以看出,所以我不确定为什么会发生这种情况。

我在应用程序中插入了断点,以查看字节数组是否实际上已填充,但只有零。 alt text

封面控制器

public ActionResult CreateCover(int id)
{
    Cover cover = new Cover();
    cover.FileID = id;
    return View(cover);
}

//
//POST: /File/CreateCover
[HttpPost]
public ActionResult CreateCover(Cover cover)
{
    cover.CoverMimeType = Request.Files["CoverUpload"].ContentType;
    Stream fileStream = Request.Files["CoverUpload"].InputStream;
    cover.CoverFileName = Path.GetFileName(Request.Files["CoverUpload"].FileName);
    int fileLength = Request.Files["CoverUpload"].ContentLength;
    cover.CoverFileContent = new byte[fileLength];
    fileStream.Read(cover.CoverFileContent, 0, fileLength);
    cover.FileID = int.Parse(Request.Form["FileID"]);
    filerepository.AddCoverData(cover);

    filerepository.Save();

    return View(cover);
    //return View("CreatePdf", "Pdf", new { id = cover.FileID });
}

CreateCover.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SampleApp.Models.Cover>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    CreateCover
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>CreateCover</h2>

    <% using (Html.BeginForm("CreateCover", "Cover", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
       <%: Html.HiddenFor(model => model.FileID) %>
    <asp:Label ID="Label2" runat="server" Text="Please Select your eBook Cover" /><br />
    <input type="file" name="CoverUpload" /><br />
    <input type="submit" name="submit" id="Submit" value="Upload" />

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

I'm having a bit of an issue uploading files to a database. The table I'm uploading to has the following structure:

dbo.Cover
CoverID           int PK
CoverFileContent  varbinary(max)
CoverMimeType     nvarchar(50)
CoverFileName     nvarchar(50)
FileID            int FK

I can upload the file using my MVC application without any errors, however in the database the file stores in CoverFileContent as "0x0000000000000000......". All the other information relating to the file, e.g. MimeType, FileName, CoverID, etc uploads correctly. I took a wild guess that this wasn't correct so I downloaded the file (created a .net MVC downloader). It seemed to download as the correct type of file, however I when I tried to open it, it told me I could not open it.

Here is the original tutorial I was following: Tutorial. I have gotten this to work perfectly, however I wanted to use ADO.net so I rewrote is slightly. I didn't make any significant changes however as can be seen from my code so I'm not sure why this is happening.

I inserted break points in my application to see if the byte array was actually being populated, which is was but only with zeros.
alt text

Cover Controller

public ActionResult CreateCover(int id)
{
    Cover cover = new Cover();
    cover.FileID = id;
    return View(cover);
}

//
//POST: /File/CreateCover
[HttpPost]
public ActionResult CreateCover(Cover cover)
{
    cover.CoverMimeType = Request.Files["CoverUpload"].ContentType;
    Stream fileStream = Request.Files["CoverUpload"].InputStream;
    cover.CoverFileName = Path.GetFileName(Request.Files["CoverUpload"].FileName);
    int fileLength = Request.Files["CoverUpload"].ContentLength;
    cover.CoverFileContent = new byte[fileLength];
    fileStream.Read(cover.CoverFileContent, 0, fileLength);
    cover.FileID = int.Parse(Request.Form["FileID"]);
    filerepository.AddCoverData(cover);

    filerepository.Save();

    return View(cover);
    //return View("CreatePdf", "Pdf", new { id = cover.FileID });
}

CreateCover.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SampleApp.Models.Cover>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    CreateCover
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>CreateCover</h2>

    <% using (Html.BeginForm("CreateCover", "Cover", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
       <%: Html.HiddenFor(model => model.FileID) %>
    <asp:Label ID="Label2" runat="server" Text="Please Select your eBook Cover" /><br />
    <input type="file" name="CoverUpload" /><br />
    <input type="submit" name="submit" id="Submit" value="Upload" />

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-10-08 09:12:05

可能是因为您没有 Close()-ing 您的 Stream

HttpPostedFileBase file = Request.Files["CoverUpload"];

cover.CoverMimeType = file.ContentType;
cover.CoverFileName = Path.GetFileName(file.FileName);
cover.FileID = int.Parse(Request.Form["FileID"]);

byte[] input = new byte[file.ContentLength];
using (Stream s = file.InputStream)
{
    s.Read(input, 0, file.ContentLength);
}

cover.CoverFileContent = input;

filerepository.AddCoverData(cover);
filerepository.Save();

return View(cover);

Probably because you're not Close()-ing your Stream

HttpPostedFileBase file = Request.Files["CoverUpload"];

cover.CoverMimeType = file.ContentType;
cover.CoverFileName = Path.GetFileName(file.FileName);
cover.FileID = int.Parse(Request.Form["FileID"]);

byte[] input = new byte[file.ContentLength];
using (Stream s = file.InputStream)
{
    s.Read(input, 0, file.ContentLength);
}

cover.CoverFileContent = input;

filerepository.AddCoverData(cover);
filerepository.Save();

return View(cover);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文