简体   繁体   中英

Download XML client-side, fetch data from Session first

My goal is to have the user be able to download some information currently stored in Session. There are multiple key/value pairs in Session I would like to have the user download as one XML file.

The control I am working with has a client-side 'onClick' event exposed. I was planning on calling a PageMethod to extract the data I wanted from Session, and return that data to the client.

I've seen a very clean implementation of this in MVC and I am wondering if this clean implementation is possible in ASP.NET AJAX, as well. If not, I am wondering what the best route would be to go for this.

In MVC/JavaScript I see something like this:

location.href = "../WorkOrders/Export/" + workOrderID;

public ActionResult Export(int id)
{
    WorkOrderPdfExporter exporter = new WorkOrderPdfExporter();
    byte[] buffer = exporter.Export(id);
    return File(buffer, "application/pdf", String.Format("workorder#{0}.pdf", id));
}

This Export method returns a FileContentResult which is an MVC thing. I am wondering if something like this exists in ASP.NET AJAX, and if the datatype is suitable to return for a Page Method.

If not, what should I be doing here? I was thinking about creating a dictionary, sticking the relevant session objects into this dictionary, serializing it to XML (I have a Serializable Dictionary class implemented), ...and then attempting to return that XML for download?

Thanks for your time.

You can write directly to HttpResponse.OutputStream - you will need to set the correct ContentType too.

The code example for OutputStream is rather verbose (mostly dealing with a dynamically created image), but if you ignore those parts, you will have a basic function that will need minor modification for your use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM