Results 1 to 6 of 6

Thread: choose file save location

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    choose file save location

    Hi,

    i have some data in a datagrid that i want to export and save as a CSV file. How do i let the user chose where to locally save the file?

    There doesn't seem to be a file dialog box to use?

    Any ideas how to go about it?

    Cheers
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    From a web application, you usually call a page that writes the binary data of the file to the response stream.

    The browser detects the MimeType from the Response stream that the file is binary, and prompts the user to Open, Save, or Cancel the file download.

    Obviously, choosing Save brings them to a file download dialog. But that is all taken care of by the browser.

    All you need to do, is create an aspx page, that takes an id as a parameter. The page will then use that id to lookup in the database where the file is located, and then all the page does is set Response.ContentType = msapp/excel (or something like that), and then Response.WriteFile the file to the output stream.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    hi nemaroller,

    thanks for the reply. The file isn't actually going to be stored on the webserver though. I want the data to go straight from the datagrid to a file on the local drive. Is this possible or will i have to create the file on the webserver first?

    Cheers
    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    It wouldn't have to a file...

    the only difference is you would use Response.Write ... and you would have to write code to get the same data you present in the grid formatted so when Response writes, its actually writing a text string to the Response stream. You could still use a MimeType that would initiate the browser to prompt a file or save as... or the user would simply click File..Save As.. Text, i guess its your call.

    so user clicks getData
    You call your special page in a new window
    The page:
    Grabs the data the same way your grid did...
    Creates a stringbuilder, writes the data formatted with commas between values and newline feeds between rows..
    And then writes the built string into the Response stream...


    the user's browser notifys them that a 'file transfer' has been initiated, would they like to open or save, and if they choose save, it of course asks them where.
    Last edited by nemaroller; Mar 11th, 2004 at 05:19 PM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    hey that works! great thanks!

    when the save box comes up though it's initial file type is aspx. Is it possible to change it in code or is that something we have to put up with.

    One little tip i found as well, make sure you delete all the html code other wise it writes that away in your file as well!

    Nick
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    just stole this off another post in this forum:

    Put this right after where you write the response.ContentType... or if you don't set the contenttype, just put it before you write your data.
    VB Code:
    1. response.AddHeader("content-disposition", "attachment;filename=MyData.csv")

Posting Permissions

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



Click Here to Expand Forum to Full Width