Transmit generated XML file without saving to disk
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, ev开发者_JAVA技巧en 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
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);
You don't mention what server technology you are using, but generally:
- Create your xml document in-memory, and write it out to your response stream.
- Change the Content-Type Header to
text/xml
- Change the Content-disposition header to
attachment; filename=data.xml;
Some links: http://en.wikipedia.org/wiki/MIME#Content-Disposition
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.
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);
But it saves all page content to file
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;
精彩评论