views:

18

answers:

1

I'm tring to write a function that need to save a file (in csv) and has a condition. it's something like that:

If the file is small then 1MB then I want to pop up an alert in javascript and then continue with the save as file commands.

What I did was:

if(...)
{
}
else
{
  ClientScript.RegisterStartupScriptBlock(...alert('aaa')...);
}
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition","attachment; filename=file.csv");
Response.TransmitFile("FILE.csv");
Response.End();

the save as works fine. but the alert wont popup in the else case. I tried RegisterStartupScript. I tried many options. But it seems that the response.end is stops the clientscript from happening.

I am writing in c# asp.net (ajax enabled website, but not in use), visual studio 2005.

Can any one knows how can I bypass this? How can I make it work?

thank you, gadym.

+1  A: 

The problem is that your content type is "text/csv" so the server will never send your startup script to the browser. Even if it did, the browser will deal with the reponse as a csv file, not as html, and so the script would never be executed.

The way to do this would be to include a redirect in your startup script, something like this:

alert("aaa");
window.loocation.href = "http:\\url-to-return-csv";

The url could point to your existing page with a parameter telling you to just retun the csv, or you could write an http handler to return the csv

Ray