开发者

Download XML client-side, fetch data from Session first

开发者 https://www.devze.com 2023-03-29 15:14 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消