我开发了一个学生信息系统。我将 PDF 文件作为每门课程的二进制文件插入到数据库中。我希望用户下载这个文件。下载文件但不显示。我在哪里做错了。
MVC代码
public FileResult FileDownload(Ders not)
{
byte[] byteArray = GetPdfFromDB(not.DersId);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
//return new FileStreamResult(pdfStream, "application/pdf");
return File(pdfStream, "application/pdf", "DersNot.pdf");
}
private byte[] GetPdfFromDB(int id)
{
#region
byte[] bytes = { };
using (SqlConnection con = new SqlConnection(@"Server=****;Database=***;UID=***;PWD=***"))
{
con.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT DersNotIcerik FROM Ders WHERE DersId=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows == true)
{
sdr.Read();
bytes = (byte[])sdr["DersNotIcerik"];
}
}
con.Close();
}
}
return bytes;
#endregion
}
看法
@Html.ActionLink(item2.DersNotAd, "FileDownload", new { id = item2.DersId })