How to download a file within a TaskPane App without using window.open()?
Asked Answered
M

1

9

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.

The browser shows a save-as dialog.

But within Microsoft Office there is no save-as dialog and the filedownload is not started.

The file is served with 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.

Millwater answered 25/2, 2016 at 16:7 Comment(7)
Download attribute in Html5 would do that but no IE and safari support. ..<a href="blob.dat" download>Designedly
Office apps, are always Internet Explorer Webviews, but I will try it.Millwater
Save dialog is a security policy and is browser specific .Designedly
Word / Powerpoint Taskpane Apps arehosted by Office within an InternetExplorer Webview. All modal dialogs are suppressed by Office.Millwater
Using iframe with ` Content-Disposition: Attachment` should work. But if office suppresses stuff.. don't you have a server file you can call and set the appropriate headersDesignedly
If you can call a file on a Web server. Then you can set the header Content-Type: and Content-Disposition in that fileDesignedly
It should work, but it don't work within Word TaskpaneApps all examples in my question blow the headline "won't work", will trigger absolute nothing in Word, as if the link wasn't clicked at all. Within a browser all examples will trigger a download-dialog.Millwater
K
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 Code ----

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,

2) instead pass some Encrypted Code or Encrypted File name itself

3) On Page where you are posting File name, Do Form Request for value hidden File ID - hidFLID

4) now use that File name and Add that File name to Response header.

5) This will not show your Actial File Name enter code here`me/File Path

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.

Kyrstin answered 6/4, 2016 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.