2016-02-25 23 views
9

Ho un'applicazione che mostra alcuni file statici da scaricare. Questa applicazione crea un iframe nascosto e imposta l'origine sull'URL del file.Come scaricare un file in un'app TaskPane senza usare window.open()?

Il browser mostra una finestra di dialogo Salva come.

Ma all'interno di Microsoft Office non esiste una finestra di dialogo Salva come e il file scaricato non è stato avviato.

Il file viene servito con Content-Disposition: Attachment. La soluzione di lavoro semplicemente aprirà una nuova istanza del browser e attiverà il download del file. Non voglio aprire una nuova finestra che si focalizzerà.

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

Penso che sia un bug serioso, se un'applicazione Web non è abilitata a seguire i collegamenti.

risposta

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

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

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

Spiegazione:

1) On You LINK pagina Non menzionano il nome del file nel tag di ancoraggio,

2) passare invece qualche codice crittografato o nome file crittografato stesso

3) Nella pagina in cui si registra Nome file, Richiesta modulo per valore nascosto ID file - hidFLID

4) ora utilizzare il nome file e aggiungere il nome file all'intestazione di risposta.

5) Questo non mostrerà il vostro Actial Nome file inserire il codice Percorso

6 here`me/File) Sopra esempio ho specificato è in Classic ASP, se si menziona il Web - la tecnologia, io possa contribuire a fornire il codice in quella tecnologia.