Do you want to save or open this file?

Discussion in 'ASP .Net' started by Derek, Nov 24, 2009.

  1. Derek

    Derek Guest

    I am trying to print a Word document from with an ASP.Net 2005 application.
    My code is:

    Dim myFile As New FileInfo(fileName)
    Response.Clear()
    Response.AddHeader("Content-Disposition", "attachment; filename=" +
    (UserSession.LoggedInUser.PrinterName + "^").Replace(" ", "$").Replace("\",
    "~") + "Barcode.Doc")
    Response.AddHeader("Content-Length", myFile.Length.ToString())
    Response.ContentType = "application/msword"
    Response.WriteFile(myFile.FullName)
    Response.Flush()

    When the code runs I am asked "Do you want to save or open this file?".

    I click open and a macro runs in the word document and word closes down.

    How can I by pass the dialog box and just print the document?
    --
    Derek
    Derek, Nov 24, 2009
    #1
    1. Advertising

  2. Derek

    Patrice Guest

    You can't for safety reasons...


    "Derek" <> a écrit dans le message de groupe
    de discussion : ...
    > I am trying to print a Word document from with an ASP.Net 2005
    > application.
    > My code is:
    >
    > Dim myFile As New FileInfo(fileName)
    > Response.Clear()
    > Response.AddHeader("Content-Disposition", "attachment; filename=" +
    > (UserSession.LoggedInUser.PrinterName + "^").Replace(" ",
    > "$").Replace("\",
    > "~") + "Barcode.Doc")
    > Response.AddHeader("Content-Length", myFile.Length.ToString())
    > Response.ContentType = "application/msword"
    > Response.WriteFile(myFile.FullName)
    > Response.Flush()
    >
    > When the code runs I am asked "Do you want to save or open this file?".
    >
    > I click open and a macro runs in the word document and word closes down.
    >
    > How can I by pass the dialog box and just print the document?
    > --
    > Derek
    Patrice, Nov 24, 2009
    #2
    1. Advertising

  3. =?Utf-8?B?RGVyZWs=?= <> wrote in
    news::

    > When the code runs I am asked "Do you want to save or open this file?".
    >
    > I click open and a macro runs in the word document and word closes down.
    >
    > How can I by pass the dialog box and just print the document?


    AFAIK, you can't do this in ASP.NET. The reason is simple: the code is on
    hte server side and the application is opening on the client side. As a
    Word doc can contain evil macros (while your print macro does not qualify
    as evil, some people are not as nice), the user should always be prompted
    so he can abort opening the document if it does not come from a trusted
    source. This does not help you, of course.

    The only option I can think of that might avoid this is setting up Word as
    a MIME type, but even then I think you would need some corporate trust
    policy to avoid save/open and force the open.

    I could be partially incorrect on this, as I have not heavily invested in
    Office/web automation, but the concept that the browser tries to save a
    user from himself is sound.

    If you want to retool so the file is printed from the server side, you have
    some options, esp. with third party office libs. This can work in a
    corporate environment, but would not work on a public website.

    Peace and Grace,

    --
    Gregory A. Beamer (MVP)

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Nov 24, 2009
    #3
  4. Derek

    Patrice Guest

    > You can't for safety reasons...

    More precisely unless workstations are under your control. If they are you
    could perhaps come up with a different scheme but you may still want to
    double check you want to implement this. If allowed by the security zone in
    which the site is, a web page could automate Word to open the file and print
    it (but you may have an additional dialog to connect to the site that holds
    the word document as Word will use its own connection)...

    I believe you can also configure this at the mime type level but then it
    would be a security issue for other sites as well...

    So it's likely better to just live with the extra click.

    --
    Patrice
    Patrice, Nov 24, 2009
    #4
  5. Derek

    Patrice Guest

    > Under no circumstances should a web page ever try to automate Word or any
    > of the other Office products.


    Not server side but client side to print to the local printer.

    That said this is mainly for completeness as IMO it would be really overkill
    to avoid an additional click and it would have its own problem likely adding
    additional clicks (such as authentication) unless you can configure every
    settings just to achieve this very specific goal.

    --
    Patrice
    Patrice, Nov 24, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Gagon

    If you could add anything you want

    John Gagon, May 12, 2006, in forum: Java
    Replies:
    55
    Views:
    1,359
    Oliver Wong
    May 25, 2006
  2. BusyBoy
    Replies:
    2
    Views:
    589
    =?Utf-8?B?TGVvbiBNYXluZQ==?=
    Oct 31, 2006
  3. tiewknvc9
    Replies:
    12
    Views:
    1,257
    Chris Uppal
    Feb 19, 2007
  4. Replies:
    0
    Views:
    271
  5. Shalom
    Replies:
    3
    Views:
    159
    s_m_b
    May 29, 2008
Loading...

Share This Page