Display Binary files from DB inside a Div tag

Last post 07-08-2009 4:54 AM by vinayuthappa. 3 replies.

Sort Posts:

  • Display Binary files from DB inside a Div tag

    07-08-2009, 2:05 AM
    • Member
      291 point Member
    • vinayuthappa
    • Member since 12-01-2008, 4:03 AM
    • Bangalore
    • Posts 151

    Hi All,

            I have a page where i am uploading pdf/word/xls files and storing in the database.

           And then i am opening it in their respective extension format.

    What i need now is

    1. upload pdf/word/xls files and storing it in the database with .txt extension
    2. Display the Binary data that is stored, inside a div tag.(earlier i was displaying it in pdf/word/xls)

    Plz Help me with this,

    Here is the code of wat i was doing earlier,

    Uploading the file and saving in DB

    protected void btnSaveDocument_Click(object sender, EventArgs e)
        {
            string DocumentName = txtDocName.Text;
            string DocumentDescription = txtDocDescription.Text;
            int len=FileUpload1.PostedFile.ContentLength;
            byte[] DocumentFile = new byte[len];
            FileUpload1.PostedFile.InputStream.Read(DocumentFile, 0, len);
            //string path = Server.MapPath(DocumentFile);
            string filename = Server.HtmlEncode(FileUpload1.FileName);
            string extension = System.IO.Path.GetExtension(filename);
            
            Documentations doc = new Documentations();
            doc.AddDocument(DocumentName, DocumentDescription, DocumentFile, extension);
        }


    Fetch the binary data from DB and display in their respective formats.

    protected void btnGetDocuments_Click(object sender, EventArgs e)
        {
            int DocumentID = Convert.ToInt32(txtDocID.Text);
            Documentations docs = new Documentations();       
            DataSet ObjDoc = new DataSet();
            ObjDoc = docs.FetchDocs(DocumentID);
            byte[] document = (byte[])ObjDoc.Tables[0].Rows[0]["Documentation"];
            //Response.BinaryWrite(document);
            string strExtenstion = ObjDoc.Tables[0].Rows[0]["Extension"].ToString();
            string DocName = ObjDoc.Tables[0].Rows[0]["DocumentName"].ToString();
            Response.Clear();
            Response.Buffer = true;
            if (strExtenstion == ".doc" || strExtenstion == ".docx")
            {
                Response.ContentType = "application/vnd.ms-word";
                Response.AddHeader("content-disposition", "inline;filename=" + DocName + ".doc");                                                        
            }
            else if (strExtenstion == ".xls" || strExtenstion == ".xlsx")
            {
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("content-disposition", "inline;filename=" + DocName + ".xls");
            }
            else if (strExtenstion == ".pdf")
            {
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "inline;filename=" + DocName + ".pdf");
            }
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(document);
            Response.End();
        }

    Uthappa T.P
    C# .Net
    Visual Studio 2008
  • Re: Display Binary files from DB inside a Div tag

    07-08-2009, 4:19 AM
    • Member
      242 point Member
    • Slicksim
    • Member since 01-29-2007, 8:25 PM
    • Lancaster, UK
    • Posts 45

    Hi,


    i am not sure you can do that, how would the web browser know how to represent your binary file?  if it was an image, you could append it to the stream.

    If i had to look at doing this, then i think i would be looking at opening the file in the respective screen shot behind the scenes, and then seeing if it is possible to screen shot it, and then appended the screen shot to the stream.


    am sure someone from the board will be able to correct me if i am wrong.


    Regards


    Si

  • Re: Display Binary files from DB inside a Div tag

    07-08-2009, 4:37 AM
    Answer
    • Member
      14 point Member
    • djrocks
    • Member since 04-25-2007, 11:29 AM
    • Posts 2

     Hi,

    Code that you has provided.Is for downloading the files.

    It is not possible to view the files like excel,pdf in Div tag.

    But it is possible using flash.Check out the sites like www.scribd.com

    HTH

  • Re: Display Binary files from DB inside a Div tag

    07-08-2009, 4:54 AM
    • Member
      291 point Member
    • vinayuthappa
    • Member since 12-01-2008, 4:03 AM
    • Bangalore
    • Posts 151

    Hi,

    Thank you for the Reply. I do not want the pdf/word to be displayed inside DIV tag. I only want the contents inside the pdf/word to be displayed in the DIV

    Uthappa T.P
    C# .Net
    Visual Studio 2008
Page 1 of 1 (4 items)