在我的应用程序中,我必须上传一个ZIP文件然后我必须使其可用,以便我们可以再次下载该文件。由于我是MVC的新手,我使用varbinary将数据存储在数据库中。 这是我的观看代码:
@using (Html.BeginForm("Upload", "Createnews", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ @ Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Createnew</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@using (Html.BeginForm("Upload", "Createnews", FormMethod.Post, new { enctype = "multipart/form-data" }))
{*@
<table>
<tr>
<td>File:</td>
<td>
<input type="file" name="UploadedFile" />
</td>
</tr>
<tr>
<td colspan="2">
@*<input type="submit" name="Upload" value="Submit" />*@
</td>
</tr>
</table>
@*}*@
@Html.LabelFor(model => model.Complete_ZIP_file, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Complete_ZIP_file, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Complete_ZIP_file, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CategoryName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CategoryName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CategoryName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SubCategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubCategoryId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SubCategoryId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SubCategoryName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SubCategoryName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SubCategoryName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@*<input type="submit" value="Create" class="btn btn-default" />*@<input type="submit" name="Upload" value="Submit" />
</div>
</div>
</div>
}
然后我在控制器中编写了以下代码: public ActionResult Upload([Bind(Include =&#34; CategoryId,Complete_ZIP_file,CategoryName,SubCategoryId,SubCategoryName&#34;)] Createnew createnew) {
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");
}
现在,我将结果存储在相应的字段中。现在,我将不得不下载它。那么,如何将这种varbinary格式再次转换为ZIP文件呢?
提前致谢。
答案 0 :(得分:0)
你不需要转换为varbinary来拉链。你可以直接写回复。
public ActionResult Download()
{
//read varbinary field from db
byte[] output = GetOutputFromDb();
return File(output, "application/zip", "fileName.zip");
}