传输生成的XML文件而不保存到磁盘
我有一个大型Web表单应用程序,我希望允许用户导出他们的数据,以防他们不想一次完成整个表单。 然后当他们返回时,他们可以导入数据并继续他们离开的地方。
这样做的部分原因是客户端的要求是不使用任何数据库。
我已经达到了创建包含所有表单数据的XML文件的程度,但我希望客户端能够下载该XML文件,而无需将应用程序保存到服务器,即使是暂时的。
是否可以创建XML文件并通过流/附件将其传输到客户端而不将其保存到磁盘?
我正在使用C#Asp.net
你可以写HttpResponse:
HttpResponse response = HttpContext.Current.Response; string xmlString = "blah "; 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);
您没有提到您正在使用的服务器技术,但通常:
- 在内存中创建xml文档,并将其写入响应流。
- 将Content-Type标头更改为
text/xml
- 将Content-disposition标头更改为
attachment; filename=data.xml;
attachment; filename=data.xml;
一些链接: http : //en.wikipedia.org/wiki/MIME#Content-Disposition
看看MemoryStream。
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx
您应该能够将XML存储在内存中,然后通过HTTP请求将其传输到客户端,或者您希望如何。
HttpResponse response = HttpContext.Current.Response; string xmlString = "
blah "; string fileName = "ExportedForm.xml"; response.StatusCode = 200; response.AddHeader("content-disposition", "attachment;filename =“+ fileName”; response.AddHeader(“Content-Transfer-Encoding”,“binary”); response.AddHeader(“Content-Length”,_ Node.Length.ToString());
response.ContentType = "application-download"; response.Write(xmlString);
但它将所有页面内容保存到文件中
是。 在PHP中它看起来像这样:
header("Content-type: text/xml"); $headerText = "Content-disposition: attachment; filename=file.xml"; header($headerText); echo $your_XML_contents; exit;