File uploading and saving to database incorrectly
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.
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>
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);
精彩评论