0

I'm creating an ASP.Net web application and I want to add an export feature. That feature will generate a string (xml, to be specific) when a button is clicked.

I want to enable the user to download this string as a file.

Do I need to create a file on the server's hard drive and open a link to that file or is there a way to download the string directly (so ASP.net takes care of creating and removing the file)?

I am currently tryig to do the following:

Response.Clear();
Response.Buffer = true;
Response.ContentEncoding = Encoding.GetEncoding(1252);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.txls");
//Multiple Response.Write(string) calls to add content to the file
Response.End();

Using firebug, I found out the the user receives the headers, but the file doesn't open.

The Internet Explorer is even able to show me the response text and has a button to save the file (both options are only avilable in the developer tools) and the file is correct, but nontheless, the file still doesn't open a window (nor a in-browser-preview).

When I use the Internet Explorer (which seems to have better Visual Studio debugging integration), a JavaScript error appears:

The EnableScriptGlobalization property cannot be changed during async postbacks or after the Init event.

Calls to Response.Write() seem to be a common cause of this issue. Of course, I call exactly that method multiple times.

I figured out that the JavaScript on the webpage (the script seems to be auto-generated) tries to parse the response, but of course it isn't formatted in the asp.NET internal communication format, but in my own XML format.

How can I tell the page to treat my page as a downloadable file?

jalgames
  • 781
  • 4
  • 23
  • 2
    Well that's awesome. What have you tried? What context are they downloading it from? A Web Forms Page? An MVC site? Web API? You need to be specific in what you're asking, providing a code example showing where the download should occur will likely help. – mason Jul 17 '14 at 13:48
  • Thanks for pointing that out, mason. I added the necessary detail to my post. Of course, if anything is missing, I'll be happy to provide it. – jalgames Jul 17 '14 at 14:10
  • Possible duplicate of [How to return XML in ASP.net?](http://stackoverflow.com/questions/543319/how-to-return-xml-in-asp-net). – mason Jul 17 '14 at 14:19

2 Answers2

1
string someXML=GetXMLContent();//I assume you have an implementation of this
Response.Clear();
Response.ContentType = "text/xml";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.xml");
Response.Write(someXML);
Response.End();

Couple of things to note. It's probably better to build up your XML string early on, then write it to the response in one shot. It makes for cleaner code, because you can separate your concerns this way.

Also, I switched the content type to "text/xml", because this is not an Excel file. Also, your file extension was incorrect. XML by convention ends in .xml. I removed the content encoding because it's not necessary most of the time. If you're dealing with weird characters feel free to add it back. Also removed the buffer in the name of removing unnecessary things.

Try this, then try opening the resulting file in your text editor and verifying the content.

mason
  • 31,774
  • 10
  • 77
  • 121
  • I added exactly the code you have there, using `Testing` instead of GetXMLContent(), just to keep it simple. A breakpoint in the last line is hit, but nothing happens. – jalgames Jul 17 '14 at 14:33
  • Where are you calling this code from? A button click handler? Please update your question to include that information. – mason Jul 17 '14 at 14:40
  • Yes, I'm calling this code from inside a button click handler – jalgames Jul 17 '14 at 14:44
  • @jalgames You need to update your question to show the relevant code. – mason Jul 17 '14 at 14:48
  • Which code? It is the only code in the button handler and the other code is exactly the code in your answer. – jalgames Jul 17 '14 at 14:49
  • OK, I did more resarch and found out some new, interesting things (see my question for details): a JavaScript error appears which says that my response can't be parsed by ASP.Net. But it shouldn't because I want the Response to be caved to the user's disk. – jalgames Jul 18 '14 at 08:44
0

The problem was that the button responsible for triggering the Excel export was not a PostBackTrigger. As soon as I had changed it, everything worked like a charm.

Nontheless, I don't know hy this fixed everything. What does it change when I set the button as a PostBackTrigger

jalgames
  • 781
  • 4
  • 23
  • Anytime you are having an issue and there's an UpdatePanel involved, you need to mention that in your question. UpdatePanels are notorious for breaking things. In fact, I recommend you never use an UpdatePanel. Since you didn't have it defined as a PostBackTrigger, it was running as an AsyncPostBackTrigger which returns data to the client side via AJAX instead of presenting it to your browser as a file to download. This is why it's so important to provide all relevant code in your question, we could have found this much more quickly. – mason Jul 18 '14 at 13:30
  • OK... I just didn't know that is thas relevant and I didn't want to post all the code (was 700+ lines), but in future, I'll remeber that I mention those things in the question... – jalgames Jul 18 '14 at 13:43
  • No, you don't post all code. You post all *relevant* code. How do you determine what's relevant? See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). By following the guidelines there you're likely to figure out the problem yourself, and if not then at least you'll make it easy for someone else to figure out your issue. – mason Jul 18 '14 at 13:46