Generate .txt file and ask the user to save it?

Last post 11-13-2009 3:05 AM by rtpHarry. 5 replies.

Sort Posts:

  • Generate .txt file and ask the user to save it?

    11-12-2009, 10:33 AM
    • Member
      629 point Member
    • levalencia
    • Member since 06-12-2006, 3:34 PM
    • Posts 281

    Hello,


    I generated a txt file, but the user requirement says, that the system must ask the user where he wants to download it.


    I generated the file this way

    File.WriteAllText(Server.MapPath(EPM.Financiera.RecaudosWeb.Soporte.Configuracion.ContextoSitioRecaudos.RutaArchivosGenerados + DateTime.Now.ToLongDateString() + ".txt"), GenerarArchivo());
             Response.Redirect(EPM.Financiera.RecaudosWeb.Soporte.Configuracion.ContextoSitioRecaudos.RutaArchivosGenerados + "archivo.txt");


    But it redirects the browser to the txt file and the user would have to manually go to the browser Save as option,


    any idea?


    Thanks

    Don't forget to click "Mark as Answer" on the post that helped you.
    Luis Esteban Valencia - Medellin - Colombia
    http://wantmvp.blogspot.com/
    MCP, MCTS SQL 2006, MCTS WEB APP, MCTS WINFORMS, MCTS DISTRIBUTED, NEXT MCPD ENTERPRISE APPLICATION DEVELOPER
  • Re: Generate .txt file and ask the user to save it?

    11-12-2009, 12:10 PM
    Answer
    • All-Star
      36,812 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,939

    Instead of redirecting them directly to the txt file you should redirect them to a httphandler which can TransmitFile() the file accross to the user. This will give them the choice to open or save the file:

  • Re: Generate .txt file and ask the user to save it?

    11-12-2009, 1:42 PM
    Answer
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Hi!

    Please check this thread. Its similar to your scenario..

    http://forums.asp.net/t/1487999.aspx

    Thanks. 

    MARK AS ANSWER if it helps
  • Re: Generate .txt file and ask the user to save it?

    11-12-2009, 4:07 PM
    Answer
    • All-Star
      36,812 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,939

    deepthoughts:

    Hi!

    Please check this thread. Its similar to your scenario..

    http://forums.asp.net/t/1487999.aspx

    Thanks. 

    One thing to remember is that with BinaryWrite() or WriteFile() options you have to load the file completly into memory first.

    You don't have to load it all into memory with the TransmitFile() example I suggested.

    In this particular example of text files there -probably- isnt much difference in it, however if you were dealing with high traffic or potentially large files you can see that this would become a memory hog and slow down your server.

  • Re: Generate .txt file and ask the user to save it?

    11-13-2009, 1:15 AM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    rtpHarry:

    deepthoughts:

    Hi!

    Please check this thread. Its similar to your scenario..

    http://forums.asp.net/t/1487999.aspx

    Thanks. 

    One thing to remember is that with BinaryWrite() or WriteFile() options you have to load the file completly into memory first.

    You don't have to load it all into memory with the TransmitFile() example I suggested.

    In this particular example of text files there -probably- isnt much difference in it, however if you were dealing with high traffic or potentially large files you can see that this would become a memory hog and slow down your server.

     

     

    Agree buddy... I was sort of more interested in showing how the file download dialog would appear rather than in the merits of different file writing methods... anyways thanks for the clarification :-)

    MARK AS ANSWER if it helps
  • Re: Generate .txt file and ask the user to save it?

    11-13-2009, 3:05 AM
    • All-Star
      36,812 point All-Star
    • rtpHarry
    • Member since 10-01-2006, 12:51 PM
    • Lincoln, England
    • Posts 5,939

    :)

Page 1 of 1 (6 items)