|
-
Mar 10th, 2004, 05:33 AM
#1
Thread Starter
Fanatic Member
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
-
Mar 10th, 2004, 11:08 AM
#2
I wonder how many charact
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.
-
Mar 11th, 2004, 12:47 PM
#3
Thread Starter
Fanatic Member
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
-
Mar 11th, 2004, 05:15 PM
#4
I wonder how many charact
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.
-
Mar 12th, 2004, 09:49 AM
#5
Thread Starter
Fanatic Member
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
-
Mar 12th, 2004, 04:49 PM
#6
I wonder how many charact
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|