launching file without knowing ContentType?

Last post 12-03-2007 2:47 PM by dbarnestx. 1 replies.

Sort Posts:

  • launching file without knowing ContentType?

    12-03-2007, 12:49 PM
    • Loading...
    • dbarnestx
    • Joined on 11-08-2007, 10:23 AM
    • Posts 6
    Hi, There’s probably an easy answer here, but it’s escaping me… I’ve got a list of documents of unrestricted types (.txt, .doc, .pdf, could be anything) stored in a database – id, filename, desc, and data blob.  I’ve got a page that lists the documents.  I need to be able to “view” the document using the default application or a separate browser window whenever the user clicks on one of the documents.  Easy enough, right? Since the documents are stored in a database, I’ve got an HTTPHandler that handles the incoming document requests, parses the query string for the doc id, and reads the database.  The doc list items are hyperlinks to a script function that does a “window.open()” passing in a url to the HTTPHandler with an appropriate query string. Two things – 1)       In the handler, is there a way to return the document in such a way that the browser will launch it (as opposed to presenting an “open/save” dialog) without setting the “Response.ContentType” to the appropriate MIME type?  Everything works fine if I hardcode “ContentType” for known types in my controlled dev environment, but I’m not going to have the MIME type available from the database and I’m trying to avoid a lookup based on file extension if possible.2)       In my controlled testing, when the browser launches a different default application for a document, say MS Word, I end up with this stranded empty browser window.  Is there a better way to launch a file from a page then my “window.open()” approach? Thanks for any ideas/opinions! Code: ClientPage.aspx:      function viewDocument(docID)      {            window.open("DocRetrieval.ashx?id=" + docID);      }  Handler.ashx:public class DocRetrieval : IHttpHandler, IRequiresSessionState{    public void ProcessRequest (HttpContext context)       {            context.Response.Clear();             int dDocID = 0;            string strDocID = context.Request["i"];            if (strDocID != null && strDocID != String.Empty)            {                  try                  {                        dDocID = ConvertCore.ToInt32(strDocID);                                                docRec = … //  database read stuff here…                         //  context.Response.ContentType = "application/msword";                        context.Response.BinaryWrite(/* byte[] */ docRec.DocumentBody);                  }                  catch                  {                        //  error handling                    }            }    }}  

     

  • Re: launching file without knowing ContentType?

    12-03-2007, 2:47 PM
    • Loading...
    • dbarnestx
    • Joined on 11-08-2007, 10:23 AM
    • Posts 6

    geez, don't know what happened to the format on post, it looked fine in my form!  Surprise

    try this...

    ~~~~~~~~~~~~~~~~~~ 

    Hi,

    There’s probably an easy answer here, but it’s escaping me…

    I’ve got a list of documents of unrestricted types (.txt, .doc, .pdf, could be anything) stored in a database – id, filename, desc, and data blob.  I’ve got a page that lists the documents.  I need to be able to “view” the document using the default application or a separate browser window whenever the user clicks on one of the documents.  Easy enough, right?

    Since the documents are stored in a database, I’ve got an HTTPHandler that handles the incoming document requests, parses the query string for the doc id, and reads the database.  The doc list items are hyperlinks to a script function that does a “window.open()” passing in a url to the HTTPHandler with an appropriate query string.

    Two things –
    1) In the handler, is there a way to return the document in such a way that the browser will launch it (as opposed to presenting an “open/save” dialog) without setting the “Response.ContentType” to the appropriate MIME type?  Everything works fine if I hardcode “ContentType” for known types in my controlled dev environment, but I’m not going to have the MIME type available from the database and I’m trying to avoid a lookup based on file extension if possible.
    2) In my controlled testing, when the browser launches a different default application for a document, say MS Word, I end up with this stranded empty browser window.  Is there a better way to launch a file from a page then my “window.open()” approach?

    Thanks for any ideas/opinions!

    Code:

    ClientPage.aspx:
     function viewDocument(docID)
     {
          window.open("DocRetrieval.ashx?id=" + docID);
     }


    Handler.ashx:
    public class DocRetrieval : IHttpHandler, IRequiresSessionState
    {
          public void ProcessRequest (HttpContext context)
          {
              context.Response.Clear();

               int dDocID = 0;
               string strDocID = context.Request["i"];
               if (strDocID != null && strDocID != String.Empty)
               {
                   try
                   {
                        dDocID = ConvertCore.ToInt32(strDocID);
        
                       docRec = … //  database read stuff here…

                       //  context.Response.ContentType = "application/msword";
                       context.Response.BinaryWrite(/* byte[] */ docRec.DocumentBody);
                   }

                   catch
                   {
                          //  error handling 
                   }
              }
         }
    }


     

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter