ASP.NET MVC附件列表(来自二进制数据)

问题描述 投票:1回答:1

使用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
1个回答
1
投票

您必须先将二进制数据保存为图像格式到目录项目中。

使用FileResult

HTML:

<div>
    @Html.ActionLink("Download", "Download", "Home");
</div>

HomeController的:

 public FileResult Download()
    {
        String path = HostingEnvironment.ApplicationPhysicalPath + "Image\\Capture.PNG";
        string fname= Path.GetFileName(path);
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        string fileName = fname;
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

    }

假设您的模型中有一个列表

 List<Attachment> lst = new List<Attachment>();

此列表是数据库中数据的内容,并将字节保存到imageformat到项目文件夹中。

你上面的代码应该落在这里用文件名保存字节,甚至id.PNG或任何。添加lst.idlst.filename用于html目的。

 public class Attachment
        {
            public int id { get; set; }
            public string filename { get; set; }
        }

你的HTML看起来像:

<table> 
@{
            if (Models.GetAttachment.lst.Count > 0)
            {
                  for (int m = 0; m < Models.GetAttachment.lst.Count; m++)
                {
                <tr>

                    <td>
                        @Html.ActionLink("Download", "Download", new { id = Models.GetAttachment.lst.Coun[m].id })
                    </td>
                </tr>
                }
            }


  }
</table>

家庭控制器与id

 public FileResult Download(int?id)
        {
            String path = HostingEnvironment.ApplicationPhysicalPath + "Image\\Capture.PNG";
            string fname= Path.GetFileName(path);
            byte[] fileBytes = System.IO.File.ReadAllBytes(path);
            string fileName = fname;
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

        }

id的目的是在每个用户点击的项目目录中找到该文件。

© www.soinside.com 2019 - 2024. All rights reserved.