Мне нужно запрограммировать этот код в методе DownloadFile, чтобы автоматически определить, что это должен быть файл типа PDF.
В настоящее время я должен нажать «Сохранить» по ссылке на скачивание в моем индексе, а затем выбрать тип файла, с которым нужно открыть файл, и затем сохранить файл. Если я не выбираю тип файла, он сохраняется как неизвестный файл.
Пожалуйста, это можно сделать заранее спасибо.
/// <summary>
/// Retrive Image from database
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult RetrieveImage(int id)
{
byte[] cover = GetImageFromDataBase(id);
if (cover != null)
{
return File(cover, "application.pdf");
}
else
{
return null;
}
}
/// <summary>
///
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public byte[] GetImageFromDataBase(int Id)
{
var q = from temp in db.Contents where temp.ID == Id select temp.Image;
byte[] cover = q.First();
return cover;
}
[HttpPost]
public FileResult DownloadFile(int? fileId)
{
byte[] bytes;
string fileName, Description;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["dbContext"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Title, Description, Image FROM [Contents] WHERE Id=@Id";
cmd.Parameters.AddWithValue("@ID", fileId);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Image"];
Description = sdr["Description"].ToString();
fileName = sdr["Title"].ToString();
}
con.Close();
}
}
return File(bytes, Description, fileName);
}
public byte[] ConvertToBytes(HttpPostedFileBase image)
{
byte[] imageBytes = null;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}