How to show saved btye pdf array in SQL Server for asp.net webform in image window?

I am developing a web application in which I need to store image files and some other documents in a database, and then I have to display them on a web page in an image window preferably. I saved the images in the database, as well as the PDF files as an array of bytes, and I used the Generic Handler to get the array of bytes from the database, and then show them in the image window, which works fine for images, but it does not work with PDF files .

public class ShowImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    { 
        Int32 imgID;
        if (context.Request.QueryString["id"] != null)
            imgID = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        byte[] buffer = ShowEmpImage(imgID);
        context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {
        dbClass db = new dbClass();
        string sql = "SELECT imgfile FROM myFiles WHERE id = @ID";
        SqlCommand cmd = new SqlCommand(sql, db.con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        object img = null;
        try
        {
            db.Connect();
            img = cmd.ExecuteScalar();
        }
        catch
        {
            return null;
        }
        finally
        {
            db.Disconnect();
        }
        return new MemoryStream((byte[])img);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

, , PDF ? , , - " ", , .

+5
2

, PDF , ... . , , "image/jpeg" , IMO. //MIME/-, , , .

PDF, , <iframe>, <img>, : a , b: . ( content-disposition, ).

+1

@Marc Gravell , , ​​ , . - . , PDF .

string sql = "INSERT INTO uploadedfiles(name, srID, policy, date, LOGpdf, fileExtension) VALUES(@p, @d, @pol, @dt, @img, @extension) SELECT @@IDENTITY";

                db.Connect();
                SqlCommand cmd = new SqlCommand(sql, db.con);
                cmd.Parameters.AddWithValue("@p", txtName.Text);
                cmd.Parameters.AddWithValue("@d", txtSrID);
                cmd.Parameters.AddWithValue("@pol", lblPolicyID.Text);
                cmd.Parameters.AddWithValue("@dt", txtDate.Text);
                if (FileUpload1.HasFile)
                {
                    cmd.Parameters.AddWithValue("@img", imgByte);
                    cmd.Parameters.AddWithValue("@extension", extension);
                }
                int id = Convert.ToInt32(cmd.ExecuteScalar());

, , , , , 100 .

<location path="UploadFiles.aspx">
    <system.web>
        <httpRuntime executionTimeout="2040" maxRequestLength="104857600"/>
    </system.web>
</location>

100 , .

+3

All Articles