0

ユーザーがリンクをクリックしたときに、ブラウザーで PDF を開こうとしています (または PDF をダウンロードしようとしています)。SO ( Open 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.
        }
    }
}
4

1 に答える 1