使用ASP.NET MVC,我可以使用jQuery GET和FilePathResult下载文件吗? [英] Using ASP.NET MVC, can I download a file using a jQuery GET and FilePathResult?

查看:81
本文介绍了使用ASP.NET MVC,我可以使用jQuery GET和FilePathResult下载文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要在文件被缓存后下载文件,然后再下载文件.我有一个jQuery.post()缓存文件,然后在文件成功缓存后调用以下命令:

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:

<script type="text/javascript">
    function startViewingFiles(fileNames) {
        $.get(
            '<%= Url.Action(MvcTemplates.Document.DisplayDownload()) %>',
            { fileName: fileNames[0] },
            function() {
                $('<div class="status fill"><p>Download complete</p></div>')
                    .appendTo('#ViewerContainer')
                    .fadeIn('slow'); 
            }
        );
    }
</script>

这与以下操作通信,因为我观察到调用实际上是在VS 2008中到达服务器的,并且FilePathResult成功退出了该方法:

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:

[HttpGet]
public virtual ActionResult DisplayDownload(string fileName)
{
    var path = _CacheService.GetCachedPath(fileName);
    return new FilePathResult(path, _MimeDictionary.LookupMimeType(fileName));
}

使用Firebug,我看到响应包含一个"500 Internal Server Error",其中抱怨System.UnauthorizedAccessException: Access to the path 'C:\websites\WebSubscriptionPortal\CacheLocation' is denied.我将VS开发服务器和IIS 7.5中的Web应用程序都配置为以我的用户身份运行,并具有对目录的完全访问权限,但我总是会收到此错误.当我拥有视图输出WindowsIdentity.GetCurrent().Name时,无论我使用哪个服务器,它都会输出我的用户名.

Using Firebug, I see the response contains a "500 Internal Server Error" that complains of a System.UnauthorizedAccessException: Access to the path 'C:\websites\WebSubscriptionPortal\CacheLocation' is denied. 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 WindowsIdentity.GetCurrent().Name, it outputs my user name regardless of which server I use.

  1. 我为什么要得到UnauthorizedAccessException?
  2. 显然,我不能使用jQuery通过FilePathResult下载文件.这是真的?
  3. 我是否需要更改客户端或服务器上的ActionResult所使用的方法以通过Javascript方法开始下载?
  1. Why am I getting the UnauthorizedAccessException?
  2. Apparently, I cannot use jQuery to download a file using FilePathResult. Is this true?
  3. Do I need to change the method used on the client or the ActionResult on the server to start the download via a Javascript method?

更新:UnauthorizedAccessException异常是由于fileNames参数为null的事实,因为未设置任何路由来映射到名为"fileNames"的方法.因此,FilePathResult构造函数的path参数只是目录名称,如异常消息中所示.

Update: The UnauthorizedAccessException exception is due the fact that the fileNames parameter is null, as no route was setup to map to a method with a parameter named "fileNames". So the path parameter to the FilePathResult constructor is simply the directory name as shown in the exception message.

推荐答案

否.您不能使用jQuery通过下载返回文件.但是,您可以将location.href设置为传递文件的操作,它将在不更改当前页面的情况下下载文件.假定FileResult是附件,通常是附件.您应该在客户端上更改方法以使用location.href而不是jQuery get.我不确定为什么会出现访问异常.可能是,虽然您有权访问特定目录,但您的帐户却无权访问路径中的中间目录之一.

No. You cannot use jQuery to return a file via download. You can, however, set location.href to the action that delivers the file and it will download it without changing the current page. This assumes that the FileResult is an attachment, which it typically is. You should change the method on the client to use location.href instead of jQuery get. I'm not sure why you are getting the access exception. It could be that while you have access to the particular directory, your account doesn't have access to one of the intervening directories in the path.

这篇关于使用ASP.NET MVC,我可以使用jQuery GET和FilePathResult下载文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆