Asp.net mvc 如何将数据库中的varbinary文件解压缩为ZIP文件?
在我的应用程序中,我必须上传一个ZIP文件,然后我必须使其可用,以便我们可以再次下载该文件。由于我是MVC新手,我使用varbinary将数据存储在数据库中。 以下是我的查看代码:Asp.net mvc 如何将数据库中的varbinary文件解压缩为ZIP文件?,asp.net-mvc,Asp.net Mvc,在我的应用程序中,我必须上传一个ZIP文件,然后我必须使其可用,以便我们可以再次下载该文件。由于我是MVC新手,我使用varbinary将数据存储在数据库中。 以下是我的查看代码: @using (Html.BeginForm("Upload", "Createnews", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() 现在,我发现结果存储在相应的字段中。现在,我必须将
@using (Html.BeginForm("Upload", "Createnews", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
现在,我发现结果存储在相应的字段中。现在,我必须将其作为下载文件提供。那么,我如何再次将这个varbinary格式转换为ZIP文件
提前感谢。您不需要将varbinary转换为zip。您可以直接写入响应
public ActionResult Download()
{
//read varbinary field from db
byte[] output = GetOutputFromDb();
return File(output, "application/zip", "fileName.zip");
}
谢谢levent和ven。文件名不是常量。它取决于用户想要的文件。另外,GetOutputFromDb…这是一个预定义的类吗?好的。Complete_ZIP_file属性只包含文件数据。保存文件时,可以通过将filename属性添加到createnew对象来将文件名保存到db。谢谢。我确实遇到编辑问题h、 当我试图在“编辑”视图中从数据库取回该文件时,它显示“内存不足异常”。我现在该怎么办?
if (Request != null)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
createnew.Complete_ZIP_file = fileBytes;
}}
if (ModelState.IsValid)
{
db.Createnews.Add(createnew);
db.SaveChanges();
return RedirectToAction("Index");
}
//return View(createnew)
return View("Create");
}
public ActionResult Download()
{
//read varbinary field from db
byte[] output = GetOutputFromDb();
return File(output, "application/zip", "fileName.zip");
}