【发布时间】:2018-06-09 10:43:35
【问题描述】:
使用 ASP.NET MVC 和 SQL Server 2016。我有附件作为二进制数据存储在 SQL Server 中。
带有数据的表格如下所示:
attachment_ID | attachment_GUID | attachment_data | attachment_name|
--------------|------------------|-----------------|----------------|
211 | A893C2Z1-4C0 | 0x255044462... | file1.doc |
212 | A893C2R5-4F0 | 0x255044455... | file5.pdf |
我需要在 html 页面中显示此附件(名称)的列表,作为超链接。因此,如果我单击附件名称,它应该会开始下载过程。
在 ASP.NET MVC 控制器方面需要帮助。
我有直接下载文件的控制器:
using System;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
namespace Project.SERVER.Controllers
{
public class AttachmenttoHTMLController : Controller
{
#region View attachments
[HttpGet]
public ActionResult Get()
{
SqlConnection connection = Project.SERVER.Classes.INT_DataBase.getConnection();
SqlCommand command = new SqlCommand("SELECT * FROM AllAttachments WHERE document_GUID='F3A2AC32-D98D'", connection);
command.Dispose();
SqlDataAdapter sqlDtAdptr = new SqlDataAdapter(command);
System.Data.DataTable result = new System.Data.DataTable();
result.Dispose();
sqlDtAdptr.Fill(result);
sqlDtAdptr.Dispose();
connection.Dispose();
connection.Close();
if (result.Rows.Count > 0 && !System.Convert.IsDBNull(result.Rows[0]["attachment_data"]))
{
byte[] file = (byte[])result.Rows[0]["attachment_data"];
Response.ContentType = result.Rows[0]["attachment_contentType"].ToString();
return File(file, Response.ContentType, result.Rows[0]["attachment_fileName"].ToString());
}
else return new EmptyResult();
}
#endregion
}
}
我应该在代码中添加什么来实现带有点击下载选项的附件列表控制器?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 hyperlink attachment