0

I am currently able to save a List to a CSV file on the server as in code below. However i want the ASP.NET application to prompt the user with a File Save Dialog box and save it in his client system. So i have this code in the 'Export to CSV' button click of the page which binds the list to a GridView. However, this code throws a JavaScript error at runtime in IE, copied below. What is wrong with this ? Should the Response be on a separate ASPX ?

 CsvContext cc = new CsvContext();
            string outputFileName = string.Format("D:\\Dashboard.csv", DateTime.Now);
            cc.Write(filteredListOfPerformanceDashboardSummary, outputFileName);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", outputFileName);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");
            HttpContext.Current.Response.Write(cc.ToString());
            HttpContext.Current.Response.End();

I get this IE error

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

when i click the link it gives an error here

function Sys$WebForms$PageRequestManager$_endPostBack(error, executor, data) {
        if (this._request === executor.get_webRequest()) {
            this._processingRequest = false;
            this._additionalInput = null;
            this._request = null;
        }
        var handler = this._get_eventHandlerList().getHandler("endRequest");
        var errorHandled = false;
        if (handler) {
            var eventArgs = new Sys.WebForms.EndRequestEventArgs(error, data ? data.dataItems : {}, executor);
            handler(this, eventArgs);
            errorHandled = eventArgs.get_errorHandled();
        }
        if (error && !errorHandled) {
            throw error;
        }
    }
Chakra
  • 2,525
  • 8
  • 43
  • 82
  • I think the UpdatePanel of Ajax is a problem. Now trying to redirect to a new page which does not have UpdatePanel. – Chakra Sep 16 '14 at 15:04

1 Answers1

1

You can do with Responce.WriteFile

CsvContext cc = new CsvContext();
string outputFileName = string.Format("D:\\Dashboard.csv", DateTime.Now);
cc.Write(categoryList, outputFileName);  
Response.Clear();
Response.ContentType = "text/csv"; //or "application/csv" 
Response.AddHeader("Content-disposition", "attachment; filename=\"sample.csv" + "\"");
Response.WriteFile(outputFileName);
Response.Flush();
Response.End();
Manish Parakhiya
  • 3,732
  • 3
  • 22
  • 25