【问题标题】:Passing a file path as string in javascript via document.location通过 document.location 在 javascript 中将文件路径作为字符串传递
【发布时间】:2016-03-29 08:08:56
【问题描述】:

我有这个功能:

    function PrintCApplication() {
        cApplication.PrintApplication(applicationID,
             function (isSuccessful, saveFileLocation) {
                 if (isSuccessful) {
                     document.location = '/CApplication/DownloadFile/' + saveFileLocation;
                 }
             });
     };

saveFileLocation 是我目录中的文件路径。我想通过 document.location 将它作为字符串传递,因为我想下载它但是当我这样做时它返回一个错误的请求(我猜这是因为反斜杠)。

saveFileLocation 值为C:/Users/Me/Documents/CApplicationTemplate.xlsx

这是我的示例网址,它不起作用。 http://localhost:4617/CApplication/DownloadFile/C:/Users/Me/Documents/CApplicationTemplate.xlsx

问题:有没有办法可以通过 document.location 将文件路径作为字符串传递?如果是怎么办?如果没有,还有其他方法吗?

【问题讨论】:

    标签: javascript c# function


    【解决方案1】:

    您只能使用 javascript 重定向到相对路径:

    window.location.href = '/MyWebsite/DownloadFile/' + saveFileLocation;
    

    请注意,这是您的 Web 应用程序目录的相对路径。您不能使用 javascript 重定向到 C:// 驱动器文件位置。为什么在这里解释:Go to local URL with Javascript

    【讨论】:

      【解决方案2】:

      假设您使用的是 IIS (Visual Studio)

      您只能访问在您的根路径网站Server.MapPath("~") 下公开的文件,并在您的 web.config 文件中添加 .xlsx 标头。

      1. 解决方案一:将文件放在根路径下,并用Server.MapPath("~/mySubfolder/CApplicationTemplate.xlsx")链接。
      2. 解决方案二:在后面的代码中处理您的文件,例如:

      在网络表单中:

      <asp:Button OnClick="btnExcel_Click" />/
      

      在代码后面:

      private void btnExcel_Click(object sender, System.EventArgs e)
      {
          var fileName = "C:/Users/Me/Documents/CApplicationTemplate.xlsx";
          System.IO.MemoryStream ms = new System.IO.MemoryStream();
          using (var fs = File.OpenRead(fileName))
          {
              ms.SetLength(fs.Length);
              fs.Read(ms.GetBuffer(), 0, Convert.ToInt32(fs.Length));
          }
      
          //Clear headers
          Response.ClearHeaders();
          Response.ClearContent();
          Response.Clear();
          //Add my send my xlsx file
          Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
          Response.AddHeader("Content-Disposition", "attachment; filename=CApplicationTemplate.xlsx");
          Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
          Response.AddHeader("Pragma", "no-cache");
          Response.AddHeader("Expires", "0");
          ms.WriteTo(Response.OutputStream);
      
          Response.Flush();
          ms.Dispose();
      }
      

      在您的 web.config 文件中添加以下配置:

      <configuration>
          <system.webServer>
              <staticContent>
                  <mimeMap fileExtension=".xslx" mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
              </staticContent>
          </system.webServer>
      </configuration>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        • 2020-10-31
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多