问题描述
我有一个有点问题文件上传到数据库中。我上传到表具有以下结构:
dbo.Cover
CoverID INT PK
CoverFileContent VARBINARY(最大值)
CoverMimeType为nvarchar(50)
CoverFileName为nvarchar(50)
FileID中INT FK
我可以使用但我的MVC应用程序没有任何错误,在数据库中CoverFileContent文件存储为上传文件0x0000000000000000 ......。所有与该文件,如其他信息Mime类型,文件名,CoverID等上传正确。我把胡乱猜测,这是不正确的,所以我下载的文件(创建一个.NET MVC下载)。它似乎下载为正确类型的文件,但是我当我试图打开它,它告诉我,我不能打开它。
下面是原来的教程中,我是以下内容:的。我得到这个工作很好,但我想用ADO.net所以我改写的咯。我没有做任何显著的变化,但是从我的code可以看出,所以我不知道为什么会这样。
我插断在我的应用点,看是否字节数组实际上被填充,它是,但只有零。
封面控制器
公众的ActionResult CreateCover(INT ID)
{
盖好盖=新盖();
cover.FileID = ID;
返回查看(盖);
}
//
//邮编:/文件/ CreateCover
[HttpPost]
公众的ActionResult CreateCover(盖罩)
{
cover.CoverMimeType = Request.Files [CoverUpload]的ContentType。
流FILESTREAM = Request.Files [CoverUpload]的InputStream。
cover.CoverFileName = Path.GetFileName(Request.Files [CoverUpload]文件名。);
INT文件长度= Request.Files [CoverUpload] CONTENTLENGTH。
cover.CoverFileContent =新的字节[文件长度]
fileStream.Read(cover.CoverFileContent,0,文件长度);
cover.FileID = int.Parse(的Request.Form [写到FileID]);
filerepository.AddCoverData(盖);
filerepository.Save();
返回查看(盖);
//返回查看(CreatePdf,PDF,新{ID = cover.FileID});
}
CreateCover.aspx
<%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage< SampleApp.Models.Cover>中%>
< ASP:内容ID =内容1ContentPlaceHolderID =TitleContent=服务器>
CreateCover
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
< H2> CreateCover< / H>
<%使用(Html.BeginForm(CreateCover,盖,FormMethod.Post,新{ENCTYPE =的multipart / form-data的}))
{%>
<%:Html.HiddenFor(型号=> model.FileID)%>
< ASP:标签ID =Label2的=服务器文本=请选择您的电子书封面/>< BR />
<输入类型=文件名称=CoverUpload/>< BR />
<输入类型=提交名称=提交ID =提交值=上传/>
<%}%GT;
< DIV>
<%:Html.ActionLink(返回目录,目录)%>
< / DIV>
< / ASP:内容>
可能是因为你不是关闭()
-ing你的流
HttpPostedFileBase文件= Request.Files [CoverUpload];
cover.CoverMimeType = file.ContentType;
cover.CoverFileName = Path.GetFileName(file.FileName);
cover.FileID = int.Parse(的Request.Form [写到FileID]);
byte []的输入=新的字节[file.ContentLength]
使用(流S = file.InputStream)
{
s.Read(输入,0,file.ContentLength);
}
cover.CoverFileContent =输入;
filerepository.AddCoverData(盖);
filerepository.Save();
返回查看(盖);
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);
这篇关于文件上传和保存到数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!