Results 1 to 5 of 5

Thread: Open documents in memory in new window

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    198

    Open documents in memory in new window

    Hi there,

    I have a some code to open a speicific path in a new window

    Code:
    DbUtils.StartTransaction();
    
                VwPersonExerciseIDResultRecord rec = VwPersonExerciseIDResultView.GetRecord("ID=" + this.ID1.Text);
    
                string filename = rec.FileName;
                byte[] contents = rec.FileContent;
                       
                // Store the contents of attachment temporarily in a file.
                // Delete the temporary file if it exists already.
                // Save to physicalPath
                string physicalPath = this.Page.Request.PhysicalApplicationPath + @"MyResults\" + filename;
    
                if (System.IO.File.Exists(physicalPath))
                {                
                    System.IO.File.Delete(physicalPath);
                }
                // Create the temporary file and store contents.
                System.IO.FileStream fs;
                fs = new System.IO.FileStream(physicalPath, System.IO.FileMode.CreateNew);
                System.IO.BinaryWriter bw;
                bw = new System.IO.BinaryWriter(fs);
                bw.Write(contents);
                bw.Flush();
                bw.Close();
                fs.Close();
                
                string filepath = this.Page.Request.Url.OriginalString;
                filepath = filepath.Replace("Result.aspx", filename);
                filepath += "#" + rec.Bookmark;
    
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "openPolicy", @"window.open('" + filepath + "','Policy')", true);
    Now what i want is to write the filecontent to a window and add the bookmark.

    Many thanks for any suggestion,
    Figa

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Open documents in memory in new window

    Would it not be easier to store that information in the database, and add the bookmark using a querystring value that represents the id of the policy stored in the database - then just dump the html stored into a literal control.

    /policy.aspx?policyid=32123"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    198

    Re: Open documents in memory in new window

    Thats is what im doing right now...

    rec is a record from the database, filecontent is data, filename is the filename(duh)

    (data can be a word document, txt file, html file)

    But i have to convert the stored data in a file open a new window, and show the file in this new window. What i want is opening this data without first saving it to a file on the server.

    Thanks for the response, Figa

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    198

    Re: Open documents in memory in new window

    Follow up:

    Instead of opening a new page, i added a page to my project and streamed the document there in the page_load event of that page. This way you can get it done from memory:

    Code:
    DatabaseRecord rec = GetYourRecordHere();
                string filename = rec.FileName;
                byte[] contents = rec.FileContent;
                
                if(filename.EndsWith(".doc"))
                    Page.Response.ContentType = "application/msword";
                else if(filename.EndsWith(".txt"))
                    Page.Response.ContentType = "text/plain";
                else if(filename.EndsWith(".pdf"))
                    Page.Response.ContentType = "application/pdf";
                else if(filename.EndsWith(".jpg") || filename.EndsWith(".jpeg"))
                    Page.Response.ContentType = "image/jpeg";
                else if (filename.EndsWith(".gif"))
                    Page.Response.ContentType = "image/gif";
                else if(filename.EndsWith(".xls"))
                    Page.Response.ContentType = "application/vnd.ms-excel";
    
                Page.Response.OutputStream.Write(contents, 0, contents.Length);
                Page.Response.End();
    This works in IE, not FireFox still trying to get that to work

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    198

    Re: Open documents in memory in new window

    Another follow-up...

    This is really annoying, everytime i want to stream an office document to the page in the page_load event, the browser opens and closes immediately. But when i test locally everything works fine.
    Also when i open the page directly, without a link, the explorer pops up a window which states if i want to open, save or cancel the page, which is fine.
    But when i do it with a link, it opens and closes. Is there a way to "prepare" the browser a specific document is being loaded?

    (I tried ContentType..didnt work for office documents)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width