简体   繁体   English

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

[英]How to download a file within a TaskPane App without using window.open()?

I have an application which shows some static files for download. 我有一个应用程序,显示一些静态文件供下载。 This application creates an hidden iframe and set the source to the file-url. 此应用程序创建隐藏的iframe并将源设置为file-url。

The browser shows a save-as dialog. 浏览器显示另存为对话框。

But within Microsoft Office there is no save-as dialog and the filedownload is not started. 但在Microsoft Office中没有另存为对话框,并且未启动filedownload。

The file is served with Content-Disposition: Attachment . 该文件与Content-Disposition: Attachment The working solution will simply open a new browser instance and trigger the file download. 工作解决方案只需打开一个新的浏览器实例并触发文件下载。 I don't want to open a new window which will gain focus. 我不想打开一个可以获得焦点的新窗口。

<!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>

I think it is a serius bug, if a web-application is unablle to follow links. 我认为这是一个严重的错误,如果网络应用程序无法跟踪链接。

<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 Code ---- ---- 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

Explanation: 说明:

1) On You Link page Don't mention Your File name in Anchor tag, 1)在您链接页面上不要在Anchor标签中提及您的文件名,

2) instead pass some Encrypted Code or Encrypted File name itself 2)改为传递一些加密代码或加密文件名本身

3) On Page where you are posting File name, Do Form Request for value hidden File ID - hidFLID 3)在您要发布文件名的页面上,执行值隐藏文件ID的表单请求 - hidFLID

4) now use that File name and Add that File name to Response header. 4)现在使用该文件名并将该文件名添加到响应头。

5) This will not show your Actial File Name enter code here`me/File Path 5)这不会显示您的Actial文件名在这里输入代码`文件路径

6) Above Example i have specified is in Classic ASP, If you mention your Web - technology, i may help to provide code in that Tech. 6)以上示例我指定的是经典ASP,如果你提到你的网络技术,我可能会帮助提供该技术的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM