【问题标题】:Download a PDF file from the server to client从服务器下载 PDF 文件到客户端
【发布时间】:2022-01-13 04:35:48
【问题描述】:

我要做的是从服务器查找共享文件夹,并在目录中找到一个文件并将其下载到我的客户端桌面。

从服务器下载到我的虚拟机桌面可以正常工作,但是,它不能下载到我的客户端桌面。它只是抛出一条错误消息,指出目标路径访问被拒绝。不知道为什么,因为当我从作为本地运行的虚拟机尝试它时,它工作得很好。但是,如果我部署到我们的开发服务器,那么它会抛出该错误消息并且不会下载到我的客户端桌面。

我研究发现由于安全问题,它永远不会下载到客户端桌面,但是有没有办法从服务器下载到客户端桌面?

#1。找到我要的文件后,从服务器获取

if (!File.Exists(thisFile))
{
     string serverSourcePath = Path.Combine(finalResourcePath + thisFile + ".pdf");

     //What I'm trying to do here is that since the client desktop path starts with C:\\Users\MyCredentials\Desktop\... just get the credentials from the user
     string userName = User.Identity.Name.Replace("CompanyName\\", "");

     string destinationFilePath = Path.Combine(@"C:\Users\" + userName + @"\Desktop\" + 
     thisFile + ".pdf");

    using (var client = new WebClient() { UseDefaultCredentials = true })
    {    
     try
      {
       client.DownloadFile(serverSourcePath, destinationFilePath);
      }
      catch (Exception ex)
      {
      return BadRequest(ex.InnerException.Message);
      }
   }
}

【问题讨论】:

  • 你说,你部署到服务器上?如果您在 Windows 和 IIS 或 Linux 和 kestrel 上运行 Web 应用程序的用户需要写入目标文件夹的权限。
  • 所以我为所有尝试从共享文件夹下载文件的用户提供了完全控制权。只要他/她在我们的域下并使用 Windows 凭据。我必须授予哪些权限?

标签: c# asp.net-mvc


【解决方案1】:

在下载之前,是否可以尝试将文件拉取到应用目录下的一个文件夹,然后下载到桌面?

【讨论】:

  • 想脱离共享目录的根目录?
  • 啊,你的意思是把它们移到 wwwroot 文件夹?
  • 是的,把它放在一个临时文件夹中,然后从那里下载。
猜你喜欢
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-22
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多