我将多个文件以字节的形式上传到数据库表中,现在如何以zip的形式下载文件?我的代码如下:
看法
@using (Html.BeginForm(Html.BeginForm("upload", "Home", FormMethod.Post, new { @id = "form", enctype = "multipart/form-data" })))
{
<input type="file" multiple="multiple" name="files" id="files" />
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</div>
}
控制器:
public FileContentResult GetFile(int id)
{
SqlDataReader rdr; byte[] fileContent = null;
string mimeType = ""; string fileName = "";
string constr = WebConfigurationManager.ConnectionStrings["DbContextModel"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
var qry = "SELECT * FROM myTable WHERE ID = @ID";
var cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("@ID", id);
con.Open();
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
fileContent = (byte[])rdr["Attachments"];
}
}
return File(fileContent, "application/zip", "download.zip");
}
模型
public partial class myTable
{
public int id {get;set;}
public byte[] Attachments { get; set; }
}
无法打开download.zip文件。“压缩的压缩文件夹无效”。请告知。提前谢谢。
上传功能:
...
byte[] bytes = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
MemoryStream target = new MemoryStream();
foreach (var file in files)
{
if (file.ContentLength > 0)
{
file.InputStream.CopyTo(target);
bytes = target.ToArray();
}
}