-1

I develop a student information system.I inser the PDF file to the database as a binary for each course.I want users to download this file.Downloading file but not displaying.Where am I doing wrong.

MvcCode

  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
    }

View

@Html.ActionLink(item2.DersNotAd, "FileDownload", new { id = item2.DersId })
Onur Önder
  • 311
  • 1
  • 7
  • 20
  • has the client got pdf installed? has IIS got the correct mime type declared? – bilpor Dec 21 '17 at 16:16
  • pdf installed.How to declare Mime type.I'm new to MVC.I do not have full control over some subjects – Onur Önder Dec 21 '17 at 16:29
  • you need to open up the IIS Management console then you should see an option for mime types. for the extension you put in `pdf` and for the mime type you enter `application/pdf` this tells IIS how to handle these types of documents. To be honest the pdf should be there as it is so common, but sometimes someone will remove it not understanding what it's for so worth checking out first. – bilpor Dec 21 '17 at 16:32
  • I checked it now.pdf attached as mime type. – Onur Önder Dec 21 '17 at 16:52

1 Answers1

1

I solved the problem like this.We can close the subject

    public FileResult FileDownload(int id)
       {

        SqlConnection con = new SqlConnection(@"Server=10UR\MSSQLSERVERR;Database=UniversiteDB;UID=onur;PWD=1234");

        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "SELECT DersNotIcerik FROM Ders WHERE DersId=@Id";
        cmd.Parameters.AddWithValue("@Id", id);
        byte[] binaryData = (byte[])cmd.ExecuteScalar();
        return File(binaryData, "application/octet-stream", "DersNot.pdf");

    }
Onur Önder
  • 311
  • 1
  • 7
  • 20