Trouble with Datagrid export CSV

6814
14
Jump to solution
02-06-2014 12:54 PM
Chang-HengYang
New Contributor III
Hi Everyone,

I found the post related to Datagrid to .csv file useful. I followed the instructions to have these codes in my web application and .ashx file. I could draw a box to select the features. However, it failed to export the file and a error message was shown in Chrome Web Console after clicking the button (Save to CSV),          Uncaught TypeError: Cannot read property 'structure' of undefined.

The web application is in jsbin

In addition, I have my web application file and MyWebHandler.ashx at the same folder

Please let me know how I fix this problem.

Many thanks,
Hank

The below code is the MyWebHandler.ashx file
public void ProcessRequest(HttpContext context)     {         context.Response.ContentType = "text/plain";         System.Collections.Specialized.NameValueCollection parameters = context.Request.Form;         string moduleName = parameters["module"];         string requestFeedback = "";         switch (moduleName)         {              case "ExportToExcel":                 EgisWeb.ExportHelper objExportCSVHelper = new EgisWeb.ExportHelper();                 objExportCSVHelper.CSV2Excel(parameters["csvdata"].ToString(), HttpContext.Current, parameters["title"].ToString());                 break;                      }         if (moduleName != "ExportToExcel")              context.Response.Write(requestFeedback);       }    public void CSV2Excel(string strCSV, HttpContext current, string strTitle)         {             current.Response.Clear();             strCSV = removeUniCodeSequenceCharacters(strCSV);             strCSV = System.Text.RegularExpressions.Regex.Unescape(strCSV);             current.Response.AddHeader("content-disposition", "attachment;filename=" + strTitle +"_"+DateTime.Now.ToString("ddmmyyyyHHMMSS") + ".csv");             current.Response.Charset = "";             current.Response.ContentType = "application/octet-stream";             current.Response.Write(strCSV);             current.Response.End();          }   private string removeUniCodeSequenceCharacters(string strWithEscapeSequence)         {             string strUse = "";             strUse = strWithEscapeSequence.Replace("%20", " ");             strUse = strUse.Replace("%2C", ",");             strUse = strUse.Replace("%27", "'");             strUse = strUse.Replace("%22", "\"");             strUse = strUse.Replace("%3F", "?");             strUse = strUse.Replace("%0A", "\n");             strUse = strUse.Replace("%28", "(");             strUse = strUse.Replace("%29", ")");             strUse = strUse.Replace("%26", "&");             strUse = strUse.Replace("%23", "#");             strUse = strUse.Replace("%3A", ":");             strUse = strUse.Replace("%21", "!");             strUse = strUse.Replace("%2D", "-");             strUse = strUse.Replace("%3B", ";");             strUse = strUse.Replace("%2A", "*");             strUse = strUse.Replace("%2B", "+");             strUse = strUse.Replace("%2E", ".");             strUse = strUse.Replace("%3C", "<");             strUse = strUse.Replace("%3D", "=");             strUse = strUse.Replace("%3E", ">");             strUse = strUse.Replace("%40", "@");             return strUse;         }
0 Kudos
14 Replies
MayJeff
Occasional Contributor

So user can just download the data to csv format and not just show the string data on the screen then have to copy and paste to save to csv format.

0 Kudos
Chang-HengYang
New Contributor III

Hi May,

I have not studies the codes on the example you provided(Edit fiddle - JSFiddle​). In addition, users still need to copy and paste and save to csv format in my application. For your requirement (users just download the data to csv format), I am still working on it. Hopefully you could let me know after you figured it out.

Thanks,

Hank

0 Kudos
MayJeff
Occasional Contributor

Sure.  I will let you know

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Chang-Heng and May Jeff,

  Marks solution is a server side code that handles the csv response format.

Attached I have the CSVHandler.ashx code that you can add to your web server.

The steps are simple

  1. Unzip the attached file and place in a new folder your create under "c:\inetpub\wwwroot\" called "HttpHandlers"
  2. In IIS Manager right click on "Default Web Site" and choose "Add Application"
  3. for the Alias type "HttpHandlers"
  4. for Physical path browse to "c:\inetpub\wwwroot\HttpHandlers"
  5. click OK
  6. Now you can add some simple code in your js app like this:

       grid.exportGrid("csv", function (str) {
          ioIframe.create('exportFrame', '', '');
          ioIframe._currentDfd = null;
          ioIframe.send({
            url: "/HttpHandlers/CSVHandler.ashx",
            content: {data: str, filename: 'MyExport.csv'}
          });
        });

Notice the url is /HttpHandlers/CSVHandler.ashx

the content contains your csv str and the desired output file name for the download.

MayJeff
Occasional Contributor

Yang,

Robert helps me on my thread.  See the answer here:

Can't get export datagrid to csv

Thanks.

0 Kudos