Incorrect loading and saving files in the database

I have a problem uploading files to the database. The table I'm loading 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 is stored in CoverFileContent as "0x0000000000000000 ......". All other information related to the file, for example. MimeType, FileName, CoverID, etc. It loads correctly. I decided that this was wrong, so I uploaded the file (created by the .net MVC loader). It seems that it loaded as the correct file type, however, when I tried to open it, I said that I could not open it.

Here is the original tutorial I read: Textbook . I got this to work fine, however I wanted to use ADO.net, so I rewrote a bit. I did not make any significant changes, as my code shows, so I'm not sure why this is happening.

I inserted breakpoints in my application to see if the byte array, which was only with zeros, really populated. 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>
+3
source share
1 answer

Probably because you are not Close()in yourStream

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);
+2
source

Source: https://habr.com/ru/post/1773684/


All Articles