当用户单击链接时,我正在尝试在浏览器中打开 PDF(或下载)PDF。我在 SO( Opening FileTable Files in c# / .net 4 )上发现了一个类似的问题,并试图在我的代码中实现答案,但没有任何运气。我真的只需要看一个完整的例子,说明如何在 ASP.NET / C# 中从 FileTable 打开文件。
代码:
public FileResult Download(int? id)
{
//start of my test code
Document document = db.Documents.Find(id);
string fileName;
byte[] fileData;
System.Guid path = document.DocumentPath;
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FT_ConnectionString"].ConnectionString))
{
string sql = "SELECT TOP 1 file_stream, name, file_type, cached_file_size FROM FTAdvisoryOpinions WHERE stream_id = @SID";
using (var command = new SqlCommand(sql, connection))
{
command.Parameters.Add("@SID", SqlDbType.UniqueIdentifier).Value = path;
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
//file_stream = 0
//name = 1
//file_type = 2
//cached_file_size = 3
long fileSize = reader.GetInt64(3); //think this might be wrong
string contentType = reader.GetString(2);
fileData = reader.GetBytes(0, 2, fileData, 0, fileSize); //says I have some invalid arguments
fileName = reader.GetString(1);
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
return File(fileData, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
else
{
connection.Close();
}
}
return null; // just returning null here til I figure it out.
}
}
}