Professional Web Applications Themes

Downloading a file to client - ASP.NET General

Hi, I have 2 problems when downloading a file from my ASP.net app to the client PC. At the moment I'm doing it with the following code: Response.ContentType = "application/octet-stream" Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileToDownload) Response.WriteFile(strFileToDownload) The problems are: 1. How can I set the name of doent to be downloaded? At the moment, the name of file is by default set to be the name of the web form that it comes from, which is far from ideal. to be able to call it, for example, myFile.doc. 2. How can I try and force the save to be in ...

  1. #1

    Default Downloading a file to client

    Hi,

    I have 2 problems when downloading a file from my ASP.net app to the client
    PC. At the moment I'm doing it with the following code:

    Response.ContentType = "application/octet-stream"

    Response.AddHeader("Content-Disposition", "attachment; filename=" +
    strFileToDownload)

    Response.WriteFile(strFileToDownload)



    The problems are:
    1. How can I set the name of doent to be downloaded? At the moment, the
    name of file is by default set to be the name of the web form that it comes
    from, which is far from ideal. to be able to call it, for example,
    myFile.doc.
    2. How can I try and force the save to be in a specific directory, or even
    to default to the Windows temp directory, or to My Doents? At the moment,
    the default target directory seems quite different on different machines.

    All help very much appreciated.

    Ian
    [email]ianxwebservices.net[/email]


    Ian Gordon Guest

  2. #2

    Default Re: Downloading a file to client

    Also you might want to look at putting a marker such as " or ' around the
    file name. If there are spaces in the filename, I don't know exactly that
    would do, but I am sure it isn't going to be desired behavior.

    Also it might be a setting on the broswer you are using. I had a web app
    for the division I am in that worked fine, except when the site was used
    outside the development group. The firm standard did not allow the filename
    to be passed to the download dialog.

    bill

    "Peter O'Reilly" <Peter_OReillytimeinc.com!N!O!.S!P!AM!> wrote in message
    news:eU6jEOuVDHA.2052TK2MSFTNGP10.phx.gbl...
    > see in line comments...
    >
    > --
    > Peter O'Reilly
    > "Ian Gordon" <xisdircon.co.uk> wrote in message
    > news:dyWVa.24$_M1.4533news.dircon.co.uk...
    > > Hi,
    > >
    > > I have 2 problems when downloading a file from my ASP.net app to the
    > client
    > > PC. At the moment I'm doing it with the following code:
    > >
    > > Response.ContentType = "application/octet-stream"
    > >
    > > Response.AddHeader("Content-Disposition", "attachment; filename=" +
    > > strFileToDownload)
    > >
    > > Response.WriteFile(strFileToDownload)
    > >
    >
    > > The problems are:
    > > 1. How can I set the name of doent to be downloaded? At the moment,
    the
    > > name of file is by default set to be the name of the web form that it
    > comes
    > > from, which is far from ideal. to be able to call it, for example,
    > > myFile.doc.
    >
    > Code looks fine. Adding the Content-Disposition HTTP Response header is
    the
    > solution. It could very well be a bug with that browser version. Have you
    > tried requesting this file with a different browser?
    >
    > Alternatively, if in your example, myFile.doc is truly a MS Word doent,
    > have you tried changing the content type to more closely match the type of
    > file, i.e. application/ms-word? That may help.
    >
    >
    > > 2. How can I try and force the save to be in a specific directory, or
    even
    > > to default to the Windows temp directory, or to My Doents? At the
    > moment,
    > > the default target directory seems quite different on different
    machines.
    > >
    > Sorry, no dice. It's this way due to security considerations. One
    solution
    > would be to create your own (perhaps 'trusted') ActiveX downloader
    control,
    > but that would be quite a bit more work to achieve such result.
    >
    >
    >

    William F. Robertson, Jr. Guest

  3. #3

    Default Re: Downloading a file to client

    Hi Ian,

    The fix is in this line:
    Response.AddHeader("Content-Disposition", "attachment; filename=" +
    System.IO.Path.GetFileName(strFileToDownload))

    The Content-Disposition header requires only the file name, not the entire
    path. When given the entire path, it negates and the browser will not set a
    name for the file download dialog.

    Also, you should call Response.Clear() before you add your headers just to
    make sure that there are no other headers being sent down to the browser.


    As for the second part of your question, no. This is a serious breach of
    security.

    HTH,

    Bill P.

    On Wed, 30 Jul 2003 22:15:55 +0100, Ian Gordon <xisdircon.co.uk> wrote:
    > Hi,
    >
    > I have 2 problems when downloading a file from my ASP.net app to the
    > client
    > PC. At the moment I'm doing it with the following code:
    >
    > Response.ContentType = "application/octet-stream"
    >
    > Response.AddHeader("Content-Disposition", "attachment; filename=" +
    > strFileToDownload)
    >
    > Response.WriteFile(strFileToDownload)
    >
    >
    >
    > The problems are:
    > 1. How can I set the name of doent to be downloaded? At the moment,
    > the
    > name of file is by default set to be the name of the web form that it
    > comes
    > from, which is far from ideal. to be able to call it, for example,
    > myFile.doc.
    > 2. How can I try and force the save to be in a specific directory, or
    > even
    > to default to the Windows temp directory, or to My Doents? At the
    > moment,
    > the default target directory seems quite different on different machines.
    >
    > All help very much appreciated.
    >
    > Ian
    > [email]ianxwebservices.net[/email]
    >
    >
    >


    --
    Using M2, Opera's revolutionary e-mail client: [url]http://www.opera.com/m2/[/url]
    Bill Priess Guest

Similar Threads

  1. Smart Client Downloading -Sattelite Assembiles
    By Venkat Kasaram in forum ASP.NET Web Services
    Replies: 0
    Last Post: March 9th, 10:05 AM
  2. Downloading file
    By Mikey in forum Macromedia Director Basics
    Replies: 1
    Last Post: April 27th, 03:29 AM
  3. Downloading file(s) from CD
    By gbt webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 1
    Last Post: December 25th, 09:31 PM
  4. Downloading a file
    By Steven in forum ASP.NET General
    Replies: 5
    Last Post: November 14th, 05:28 PM
  5. Downloading files from the server to the client browser
    By rogerwithnell webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 11th, 05:19 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139