title: Using ASP.NET MVC, can I download a file using a jQuery GET and FilePathResult?
body: <p>My app needs to download a file after the file has been cached and then download the file. I have one jQuery.post() cache the file and then call the following after the file is successfully cached:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      function startViewingFiles(fileNames) {
          $.get(
              '&lt;%= Url.Action(MvcTemplates.Document.DisplayDownload()) %&gt;',
              { fileName: fileNames[0] },
              function() {
                  $('&lt;div class="status fill"&gt;&lt;p&gt;Download complete&lt;/p&gt;&lt;/div&gt;')
                      .appendTo('#ViewerContainer')
                      .fadeIn('slow'); 
              }
          );
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>This communicates with the following action, as I have observed the calls actually make it to the server in VS 2008 and the FilePathResult exits the method successfully:</p>
  
  <pre><code>[HttpGet]
  public virtual ActionResult DisplayDownload(string fileName)
  {
      var path = _CacheService.GetCachedPath(fileName);
      return new FilePathResult(path, _MimeDictionary.LookupMimeType(fileName));
  }
  </code></pre>
  
  <p>Using Firebug, I see the response contains a "500 Internal Server Error" that complains of a <code>System.UnauthorizedAccessException: Access to the path 'C:\websites\WebSubscriptionPortal\CacheLocation' is denied.</code> I configured both the VS development server and the web app in IIS 7.5 to run as my user with full access to the directory, but I always get this error. When I have the view output <code>WindowsIdentity.GetCurrent().Name</code>, it outputs my user name regardless of which server I use.</p>
  
  <ol>
  <li>Why am I getting the <code>UnauthorizedAccessException</code>?</li>
  <li>Apparently, I cannot use jQuery to download a file using <code>FilePathResult</code>. Is this true? </li>
  <li>Do I need to change the method used on the client or the <code>ActionResult</code> on the server to start the download via a Javascript method?</li>
  </ol>
  
  <p><strong>Update:</strong> The <code>UnauthorizedAccessException</code> exception is due the fact that the <code>fileNames</code> parameter is null, as no route was setup to map to a method with a parameter named "fileNames". So the <code>path</code> parameter to the <code>FilePathResult</code> constructor is simply the directory name as shown in the exception message.</p>