代码之家  ›  专栏  ›  技术社区  ›  flipdoubt

使用ASP.NET MVC,我可以使用jQuery GET和FilePathResult下载文件吗?

  •  1
  • flipdoubt  · 技术社区  · 16 年前

    <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成功地退出了该方法:

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

    System.UnauthorizedAccessException: Access to the path 'C:\websites\WebSubscriptionPortal\CacheLocation' is denied. 我在iis7.5中配置了vsdevelopment服务器和web应用程序,以完全访问目录的用户身份运行,但我总是遇到这个错误。当我有了视图输出 WindowsIdentity.GetCurrent().Name

    1. 为什么我会得到 UnauthorizedAccessException
    2. 显然,我不能使用jQuery下载文件 FilePathResult . 这是真的吗?
    3. ActionResult 在服务器上通过Javascript方法开始下载?

    更新: 这个 未授权访问例外 例外情况是由于 fileNames path 文件路径结果 构造函数只是异常消息中显示的目录名。

    1 回复  |  直到 16 年前
        1
  •  4
  •   tvanfosson    16 年前

    location.href 到传递文件的操作,它将在不更改当前页的情况下下载该文件。这假设FileResult是一个附件,通常是这样。您应该更改客户端上要使用的方法 位置.href get . 我不知道你为什么会遇到访问异常。可能是,当您可以访问特定目录时,您的帐户无法访问路径中的一个中间目录。