如何在TaskPane应用程序中下载文件而不使用window.open()?

9
我有一个应用程序可以显示一些静态文件以供下载。该应用程序创建了一个隐藏的iframe并将源设置为文件URL。
浏览器会显示一个另存为对话框。
但是,在Microsoft Office中没有另存为对话框,文件下载也不会开始。
该文件使用“Content-Disposition:Attachment”提供服务。有效的解决方案是打开一个新的浏览器实例并触发文件下载。我不想打开一个新窗口,因为那样会获得焦点。
<!DOCTYPE html>
<html>
    <head>
        <script>
            function di(){
                document.getElementById("d").src="blob.dat";
            }
        </script>
        <title>download</title>
    </head>
    <body>
        <h1>file loading</h1>
        <h2>works</h2>
        <p>But opens a new window</p>
        <a href="blob.dat" target="_blank"> a blank </a><br>
        <a href="blob.dat" target="download"> named frame </a>
        <h2>won't work</h2>
        <a href="blob.dat"> a self </a><br>
        <a href="blob.dat" target="_self"> a self </a><br>
        <a href="blob.dat" target="_top"> a top </a><br>
        <a href="#" onclick="di();"> iframe </a><br><br>
        <iframe id="d"></iframe>
    </body>
</html>

我认为如果一个Web应用程序无法跟随链接,这是一个严重的漏洞。


1
Html5 中的下载属性可以做到这一点,但 IE 和 Safari 不支持。..<a href="blob.dat" download> - Reagan Gallant
1
办公应用程序始终是Internet Explorer Webviews,但我会尝试它。 - Christian Kuetbach
1
保存对话框是一项安全策略,并且与浏览器相关。 - Reagan Gallant
1
Word / Powerpoint 任务窗格应用程序由 Office 在 Internet Explorer Webview 中托管。所有模态对话框都被 Office 抑制。 - Christian Kuetbach
1
使用带有“Content-Disposition: Attachment”的iframe应该可以工作。但是如果Office抑制了内容...你没有一个可以调用并设置适当标头的服务器文件吗? - Reagan Gallant
显示剩余2条评论
1个回答

1
<script language="javascript">
function OpenADocument(strDoc)
{

        document.blob.hidFLID.value=strDoc;
        document.blob.action = "OpenLinksDocument.asp";
        document.blob.method="post"
        document.blob.submit(); 
}
</script>

---- ASP 代码 ----

Private Sub DownloadFile(file, MainFileName)
   '--declare variables
   Dim strAbsFile
   Dim strFileExtension
   Dim objFSO
   Dim objFile
   Dim objStream, FileNM
   strAbsFile = Server.MapPath(file)
   Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
   If objFSO.FileExists(strAbsFile) Then
      Set objFile = objFSO.GetFile(strAbsFile)
      strFileExtension = LCase(objFSO.GetExtensionName(file))

      '-- first clear the response, and then set the appropriate headers
      Response.Clear

      '-- the filename you give it will be the one that is shown ' to the users by default when they save

      dim NewFileName
     NewFileName= "RandomFileNameYouWishtoGive" & Session.SessionID &"." &strFileExtension

      Response.AddHeader "Content-Disposition", "attachment; filename=" & NewFileName
      Response.AddHeader "Content-Length", objFile.Size
      Response.ContentType = "application/octet-stream"

      Set objStream = Server.CreateObject("ADODB.Stream")
      objStream.Open
      '-- set as binary
      objStream.Type = 1
      Response.CharSet = "UTF-8"
      '-- load into the stream the file
      objStream.LoadFromFile(strAbsFile)
      '-- send the stream in the response
      Response.BinaryWrite(objStream.Read)
      objStream.Close
      Set objStream = Nothing
      Set objFile = Nothing
   Else 'objFSO.FileExists(strAbsFile)
      Response.Clear
      Response.Write("No such file exists.")
   End If
   Set objFSO = Nothing
End Sub

解释:

1)在您的链接页面上,不要在锚点标记中提及您的文件名,而是传递一些加密代码或加密文件名本身

2)在您发布文件名的页面上,使用隐藏文件ID值的表单请求 - hidFLID

3)现在使用该文件名并将该文件名添加到响应头中。

4)这将不会显示您的实际文件名enter code here`me / File Path

5)上面的示例是在Classic ASP中指定的,如果您提及您的Web技术,我可以帮助提供该技术的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接