【问题标题】:save an pdf file in local folder or downloads将 pdf 文件保存在本地文件夹中或下载
【发布时间】:2021-08-28 03:34:53
【问题描述】:

我正在使用以下代码将 pdf 文件保存在特定文件夹中。如果文件夹不存在,它将自动创建一个文件夹。它在本地运行良好。但是当我将网站发布到 IIS 时。它不起作用。

  string folderPath = "D:\\Labels\\";
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                string file = @"D:\Labels\" + DateTime.Today.ToString("dd-MM-yyhh-mm-ss") + ".pdf" + "";
                var fileStream = File.Create(file);
                // change file name for PNG images
                responseStream.CopyTo(fileStream);
                responseStream.Close();
                fileStream.Close();
                ShowMessage("Downloaded Sucessfully", MessageType.Success);

【问题讨论】:

  • 确保目录对 IIS 用户有足够的权限。还要检查驱动器,即网络服务器中的“D”。
  • 此代码在服务器端运行。你的服务器有驱动器吗?
  • 我想将文件保存在曾经使用该网站的本地
  • @VignaHariKarthik 这不可能。您的代码在服务器端运行,并且只能访问服务器端资源。这在客户端的 javascript 中也是不可能的,因为如果网站可以访问本地硬盘驱动器,那将是一个巨大的安全问题。除此之外,想象一下如果使用 MacOS、Linux 或智能手机的用户访问您的网站会发生什么。这些系统会对D:\... 这样的路径做什么?即使是现在大多数 Windows 机器都没有 D: 驱动器了......
  • 通过您的本地部署,它似乎可以工作。但这只是因为服务器和客户端都在同一台机器上运行

标签: c# asp.net


【解决方案1】:

Ι want to save the file in the local who ever use the website

这是不可能的——你可以提供一个下载文件的链接——你不能从服务器在客户端运行File.函数。

File. 函数仅适用于服务器端,并且仅当您对目录具有权限时。

How to set correct file permissions for ASP.NET on IIS
file download by calling .ashx page
What is the best way to download file from server

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    相关资源
    最近更新 更多