ASP.NET MVCとSQL Server 2016の使用SQL Serverにバイナリデータとして保存されている添付ファイルがあります。ASP.NET MVC添付ファイル一覧(バイナリデータから)
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
}
}
は私がクリックしたときにダウンロードするオプションで添付ファイルリストのためのコントローラを達成するためのコードに何を追加する必要がありますか?
あなたの 'Capture.PNG'がプロジェクトディレクトリの' Image'フォルダの中にあると仮定します。 –
DBからたくさんのファイルがある場合は、ループを使ってhtmlに追加して、各ファイルごとにリンクを作成することができます。 –
それを見つけてそれを再現するには、しばらく時間がかかりました。この解決策は、助けてくれてありがとう! – Dmitry