title: How to save file to local hdd using asp or other script running naturally on IIS
body: <p>I am writing Dojo web application that has a dojo enhanced datagrid. After the datagid is populated with data, i have option that saves the file to the user's machine. To do this i am using Dojo csv <a href="https://dojotoolkit.org/reference-guide/1.8/dojox/grid/EnhancedGrid/plugins/Exporter.html" rel="nofollow">exporter</a> that creates a string from the datagrid in CSV format. In the following code i am passing that string to a csv.php script that allows me to save the string as csv file.</p>
  
  <p>The code using the dojo exporter:</p>
  
  <pre><code>var g = dijit.byId("grid");
  g.exportGrid("csv", {
      writerArgs: {
          separator: ","
      }
  }, function(str){
         debugger;
       //the code below is used if the user prefers to have call to the php script on a php enabled server
       var form = document.createElement('form');
       dojo.attr(form, 'method', 'POST');
       document.body.appendChild(form);
       dojo.io.iframe.send({
       url: "csv.php",
       form: form,
       method: "POST",
       content: {exp: str},
       timeout: 15000
       });
  
       document.body.removeChild(form);
  });
  </code></pre>
  
  <p>The php code for csv.php:</p>
  
  <pre><code>&lt;?
  $time = time();
  header("Pragma: public");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Content-type: application/csv");
  header("Content-Disposition: attachment; filename=\"grid_$time.csv\"");
  $exportedData = $_POST['exp'];
  echo stripslashes($exportedData);
  exit;
  ?&gt; 
  </code></pre>
  
  <p>So the problem i have is that i am using php and that requires using php enabled server. If the person deploying the application has such a server, no problem the code works. however, if he uses IIS installation of php and specific settings  in IIS are needed. 
  I want to write code that can be run on IIs without extra set up. I was thinking tht ASP is the way to go, but i am open to any suggestions and help. How can i rewrite that csv.php code in asp.</p>
  
  <p>Thanks</p>
  
  <p>If something is wrong with my question please do not remove it, but let me know what to change.</p>
  
  <p>Update:</p>
  
  <p>This is the code i have in the ashx page, but now i get the "500 Internal Server Error". I have the asp.net installed on IIS, so it it is not the problem. In the js file i only changed scv.php to csv.ashx. Do i have to use different code to call the ashx?</p>
  
  <pre><code>   &lt;%
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.IO;
  
  namespace WebApplication2
  {
  
      public class Handler1 : IHttpHandler
      {
  
          public void ProcessRequest(HttpContext context)
          {
           context.Request.Files(0);
              Dim reader = new IO.StreamReader(context.Request.InputStream);
              Dim string = reader.ReadToEnd();
              context.Response.ContentType = "application/csv";
              context.Response.AddHeader("Content-Disposition", "attachment; filename=Whatever.csv");
              context.Response.AddHeader("content-length", context.Request.ContentLength.ToString());
  
              Response.Write("something");
              if( context.Response.IsClientConnected )
                  context.Response.Flush();
          }
  
          public bool IsReusable
          {
              get
              {
                  return false;
              }
          }
      }
  }
  %&gt;
  </code></pre>