How to force download ?

Discussion in 'ASP General' started by fredda054@gmail.com, May 23, 2006.

  1. Guest

    Hi !
    I have a problem with downloading csv files from a web app.
    The files get created in the right directory, so I know they're there.
    I get the error "The page cannot be found" when I click the download
    link. Here's the code that does the work. (hopefully the format doesn't
    get too screwed up here :) The code looks pretty straight forward, but
    I can't understand what's wrong with it. It should download the file,
    if it's there...
    Greatfull for any help !

    Fredrik

    --------------------------------------------------------------------------------------------------------------------------------

    public static void WriteFile(byte[] fileArr, string fileName)
    {
    RemoveFiles();

    string path = HttpContext.Current.Server.MapPath("/Reports/" +
    fileName);

    //Create a filestream to send to the user
    FileStream fStream = new FileStream(path, FileMode.Create,
    FileAccess.Write);

    try
    {
    Log.Message(EventCategory.Web, "Writing to user : " + path);

    fStream.Write(fileArr, 0, fileArr.Length);
    }
    catch (Exception ex)
    {
    CpLog.Exception(EventCategory.Web, ex);
    }
    finally
    {
    fStream.Close();
    }
    }

    public static void UploadFile(string fileName)
    {
    try
    {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AppendHeader("Content-Disposition",
    "attachment; filename=" + fileName);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private);

    string filePath =
    ConfigurationSettings.AppSettings["ReportPath"].ToString();
    Log.Message(EventCategory.Web, "Writing file to : " + filePath);
    Log.Message(EventCategory.Web, "HttpHeader : " +
    HttpContext.Current.Request.Headers.ToString());
    HttpContext.Current.Response.Redirect(filePath + fileName);
    }
    catch(Exception ex)
    {
    Log.Exception(EventCategory.Web, ex);
    }
    finally
    {
    HttpContext.Current.Response.End();
    }

    }
     
    , May 23, 2006
    #1
    1. Advertising

  2. Mike Brind Guest

    wrote:
    > Hi !
    > I have a problem with downloading csv files from a web app.
    > The files get created in the right directory, so I know they're there.
    > I get the error "The page cannot be found" when I click the download
    > link. Here's the code that does the work. (hopefully the format doesn't
    > get too screwed up here :) The code looks pretty straight forward, but
    > I can't understand what's wrong with it. It should download the file,
    > if it's there...
    > Greatfull for any help !
    >
    > Fredrik
    >
    > --------------------------------------------------------------------------------------------------------------------------------
    >
    > public static void WriteFile(byte[] fileArr, string fileName)


    This newsgroup covers classic asp. DotNet is a different technology.
    Try microsoft.public.dotnet.framework.aspnet.

    --
    Mike Brind
     
    Mike Brind, May 23, 2006
    #2
    1. Advertising

  3. Guest

    Ooops, sorry, my misstake. I've used this group alot before, but kind
    of forgot that it's all about classic ASP.



    skrev:

    > Hi !
    > I have a problem with downloading csv files from a web app.
    > The files get created in the right directory, so I know they're there.
    > I get the error "The page cannot be found" when I click the download
    > link. Here's the code that does the work. (hopefully the format doesn't
    > get too screwed up here :) The code looks pretty straight forward, but
    > I can't understand what's wrong with it. It should download the file,
    > if it's there...
    > Greatfull for any help !
    >
    > Fredrik
    >
    > --------------------------------------------------------------------------------------------------------------------------------
    >
    > public static void WriteFile(byte[] fileArr, string fileName)
    > {
    > RemoveFiles();
    >
    > string path = HttpContext.Current.Server.MapPath("/Reports/" +
    > fileName);
    >
    > //Create a filestream to send to the user
    > FileStream fStream = new FileStream(path, FileMode.Create,
    > FileAccess.Write);
    >
    > try
    > {
    > Log.Message(EventCategory.Web, "Writing to user : " + path);
    >
    > fStream.Write(fileArr, 0, fileArr.Length);
    > }
    > catch (Exception ex)
    > {
    > CpLog.Exception(EventCategory.Web, ex);
    > }
    > finally
    > {
    > fStream.Close();
    > }
    > }
    >
    > public static void UploadFile(string fileName)
    > {
    > try
    > {
    > HttpContext.Current.Response.Clear();
    > HttpContext.Current.Response.AppendHeader("Content-Disposition",
    > "attachment; filename=" + fileName);
    > HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private);
    >
    > string filePath =
    > ConfigurationSettings.AppSettings["ReportPath"].ToString();
    > Log.Message(EventCategory.Web, "Writing file to : " + filePath);
    > Log.Message(EventCategory.Web, "HttpHeader : " +
    > HttpContext.Current.Request.Headers.ToString());
    > HttpContext.Current.Response.Redirect(filePath + fileName);
    > }
    > catch(Exception ex)
    > {
    > Log.Exception(EventCategory.Web, ex);
    > }
    > finally
    > {
    > HttpContext.Current.Response.End();
    > }
    >
    > }
     
    , May 24, 2006
    #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. PJS

    Force Download - XML problem

    PJS, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    533
  2. Ken Cox [Microsoft MVP]

    Re: Force File Download XML Problem

    Ken Cox [Microsoft MVP], Jul 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    719
  3. Miguel Dias Moura

    How to force download?

    Miguel Dias Moura, Apr 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    586
    Dan Brussee
    Apr 19, 2004
  4. localhost

    Force Download Not Working

    localhost, Jun 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    385
    Steven Cheng[MSFT]
    Jun 30, 2004
  5. tma

    Force download rather than view

    tma, Aug 7, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    850
    =?Utf-8?B?SmVyZW15IERhdmlz?=
    Aug 8, 2004
Loading...

Share This Page