views:

121

answers:

4

I have a large web form application that I want to allow the user to export their data in case they don't want to complete the entire form at once. Then when they return they can import the data and continue where they left off.

The reason for this is part of the requirement for the client is that no database is to be used.

I've gotten to the point where I create an XML file containing all the form data, but I want the client to be able to download that XML file without the application having to save it to the server, even temporarily.

Is it possible to create the XML file and transmit it via stream/attachment to the client without saving it to disk?

I'm using C# Asp.net

+1  A: 

Look into MemoryStream.

http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx

You should be able to store the XML in memory, then pipe it to your client through the HTTP Request or however you would like.

Ian Jacobs
A: 

yes. In PHP it looks like this:

header("Content-type: text/xml");
$headerText = "Content-disposition: attachment; filename=file.xml";
header($headerText); 
echo $your_XML_contents;
exit;
tloach
Oops, I forgot to mention I'm using C# Asp.net
TruthStands
Then pay more attention to mlsteeves' comment, same thing but his is language-agnostic :D
tloach
+2  A: 

You don't mention what server technology you are using, but generally:

  1. Create your xml document in-memory, and write it out to your response stream.
  2. Change the Content-Type Header to text/xml
  3. Change the Content-disposition header to attachment; filename=data.xml;

Some links: http://en.wikipedia.org/wiki/MIME#Content-Disposition

mlsteeves
+1  A: 

You can write to the HttpResponse:

        HttpResponse response = HttpContext.Current.Response;

        string xmlString = "<xml>blah</xml>";
        string fileName = "ExportedForm.xml";

        response.StatusCode = 200;

        response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        response.AddHeader("Content-Transfer-Encoding", "binary");
        response.AddHeader("Content-Length", _Buffer.Length.ToString());

        response.ContentType = "application-download";
        response.Write(xmlString);
Cory Charlton
Note that I specifically use a ContentType of "application-download" rather than a more accurate mime type of "application/xml" or "text/xml". This is to overcome an issue in IE6 where the filename in the "content-disposition" header is not used for the default "Save As" filename.
Cory Charlton
Worked like a charm. Thanks!
TruthStands