Question

I have an ASP.NET Web Forms application. I would like to create a download link to make available to the user the possibility to download an XML file. However the file does not have to be stored on the server.

In my aspx file I have the download link (placed inside a GridView):

 <asp:HyperLinField Text="Download" DataNavigateUrlFormatString="download.aspx?ProductId={0}" DataNavigateUrlFields="ProductId">

In the download.aspx.vb page:

Dim productId As String = Request.QueryString("productId")
Dim xmlDoc As String = _ProductServices.GetXmlDocPerId(productId)
Dim xdoc As XmlDocument = New XmlDocument()
xdoc.LoadXml(xmlLicense)

Now I would like to create a file, place the XML content inside and deliver it to the user without saving it to the server. Shall I use a MemoryStream combined with a StreamReader?

Was it helpful?

Solution

You don't need to create the file - you already have the XML content in your XmlDocument, which you can directly output to the browser.

Untested example:

Response.ContentType = "application/xml"
Response.Clear()

xdoc.Save(Response.OutputStream)
Response.Flush()

Update:

To get the browser to show the download dialog, you simply need to add a content-disposition header:

Response.AddHeader("Content-Disposition", "attachment; filename=some_name.xml");

OTHER TIPS

It can be as simple as:

Private Sub SendResults()
         'Write the XML for the DataSet.
         Page.Response.ContentType = "text/xml"
         Page.Response.Output.Write(xmlContentAsString)
         Page.Response.End()
    End Sub

Essentially, you change the response output type, and pass to the output the xmldocument content. You don't actually create a file in the system, but instead stream the response to the client.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top