Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 781
Christian Kuetbach Avatar asked Feb 25 '16 16:02

Christian Kuetbach


1 Answers

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

like image 116
Ravindranath K. Avatar answered Oct 22 '22 07:10

Ravindranath K.