Downloading a file to client

Discussion in 'ASP .Net' started by Ian Gordon, Jul 30, 2003.

  1. Ian Gordon

    Ian Gordon Guest

    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 document 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 Documents? At the moment,
    the default target directory seems quite different on different machines.

    All help very much appreciated.

    Ian
     
    Ian Gordon, Jul 30, 2003
    #1
    1. Advertising

  2. 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" <!N!O!.S!P!AM!> wrote in message
    news:...
    > see in line comments...
    >
    > --
    > Peter O'Reilly
    > "Ian Gordon" <> wrote in message
    > news:dyWVa.24$...
    > > 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 document 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 document,
    > 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 Documents? 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., Jul 30, 2003
    #2
    1. Advertising

  3. Ian Gordon

    Bill Priess Guest

    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 <> 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 document 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 Documents? At the
    > moment,
    > the default target directory seems quite different on different machines.
    >
    > All help very much appreciated.
    >
    > Ian
    >
    >
    >
    >




    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Bill Priess, Jul 30, 2003
    #3
    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. RC
    Replies:
    3
    Views:
    362
    Eliyahu Goldin
    Jan 19, 2005
  2. =?Utf-8?B?VmVua2F0IEthc2FyYW0=?=

    Smart Client Downloading -Sattelite Assembiles

    =?Utf-8?B?VmVua2F0IEthc2FyYW0=?=, Mar 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    368
    =?Utf-8?B?U3RlZmFhbiBWLg==?=
    Mar 10, 2005
  3. Rob Nicholson
    Replies:
    3
    Views:
    469
    Steven Cheng[MSFT]
    May 13, 2005
  4. Cathryn  Johns

    Downloading file to client

    Cathryn Johns, Oct 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,816
  5. Replies:
    1
    Views:
    359
    Joerg Jooss
    Dec 3, 2005
Loading...

Share This Page