Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

Can you Save a doc loaded in an iframe?

I have a document (xls or ppt) loaded in an iframe.  Can I have a button outside of the iframe allow the user to Save that document?
0
econy1
Asked:
econy1
  • 6
  • 3
1 Solution
 
JakobACommented:
lets rephrase the question:
    Can I use some javascript code to save a virusfile on the clients PC when he visit my website.
Right. that MUST NOT be possible.

And so, eventhough you are a nice guy and just want to save a harmless file, that action has to be illegal. It must be the users decision if he want to save your file on his PC.

so make a link pointing to your file
   <a href="http://yourplace.com/yourfile.doc">my essay on breeding spotted garter snakes</a>
and tell the user how he can dovnload it if he wants. (right click and save target as)

regards JakobA
0
 
ZontarCommented:
<a href="myfile.ppt" title="Right-click or control-click to save this file">Powerpoint File</a>

That's the simplest way. Doesn't depend on any scripting tricks or anything.
0
 
econy1Author Commented:
jakoba - you have no right making aligations like that.  you are absolutely incorrect in your assumption.  my project is an intranet site where the published content consists of pdf/xls/ppt documents.  when the pdf loads in the iframe, the acrobat toolbar is available which offers the Save option, Print option, etc.  however, with the other file types - xls/ppt - no toolbar loads.  i want to provide a simple way for the user to save that file.  I'M LOOKING FOR A SAVE AS DIALOG.  for users who have not set their browsers to automatically load these filetypes, they will be given the dialog allowing them to choose Save.  But other users will simply see the file open up in a new browser window.  i would appreciate any constructive feedback.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JakobACommented:
I made neither assumptions or allegations.
Please read and understand before flaming like that.
0
 
ZontarCommented:
First of all, don't jump down JA's throat... if you actually read his post, you'll see he wasn't accusing YOU of anything, he was pointing out that being able to force downloads via JavaScript would be a major security problem.

Now... What is your server running? PHP? ASP? Python Server Pages? And what kind of server? IIS Apache?

This is because if you want to prompt with a "Save As..." dialogue, you must present the file to the user's Web browser as content-type application/octet-stream. You can't do this with JavaScript alone (see above), because content types are determined by the Web server, and by the Web server only.

Using JavaScript + PHP, this is how I'd do that:

1. Your iframe & button:
<iframe id="myIFrame" src="myfile.ppt">
<input type="button" value="Download The File" onclick="doDownload();">
<script type="text/javascript">
  function doDownload()
  {
    var file = document.getElementById("myIFrame").src;
    self.location.href = "download.php?file=" + file;
  }
</script>

2. PHP file (download.php):
<?php
  $file = $_GET["file"];
  $data = file( realpath($file) );

  header("Content-Type: application/octet-stream");
  header("Content-Disposition: attachment; filename=$file");
  print($data);
?>

JS + ASP version...
1. iframe/button code:

<iframe id="myIFrame" src="myfile.ppt">
<input type="button" value="Download The File" onclick="doDownload();">
<script type="text/javascript">
  function doDownload()
  {
    var file = document.getElementById("myIFrame").src;
    self.location.href = "download.asp?file=" + file;
  }
</script>

2. ASP page (download.asp)

<%@LANGUAGE="VBSCRIPT"%>
<%
FileName = Request.QueryString("file");
FilePath = Server.MapPath(FileName)

Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","attachment; filename=" & FileName

Set Stream = Server.CreateObject("ADODB.Stream")
Stream.Open()
Stream.Type = 1
Stream.LoadFromFile(filePath)
Response.BinaryWrite Stream.Read()
Stream.Close
SET adoStream = Nothing
Response.End
%>
0
 
ZontarCommented:
Whoops, let me try that ASP file again... I usually do my ASP coding in JScript... anyway...

<%@LANGUAGE="VBSCRIPT"%>
<%
FileName = Request.QueryString("file");
FilePath = Server.MapPath(FileName)

Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","attachment; filename=" & FileName

Set Stream = Server.CreateObject("ADODB.Stream")
Stream.Open()
Stream.Type = 1
Stream.LoadFromFile(FilePath)
Response.BinaryWrite Stream.Read()
Stream.Close
Set Stream = Nothing
Response.End
%>
0
 
ZontarCommented:
Oh bugger -- One... More... Time...

<%@LANGUAGE="VBSCRIPT"%>
<%
FileName = Request.QueryString("file")
FilePath = Server.MapPath(FileName)

Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","attachment; filename=" & FileName

Set Stream = Server.CreateObject("ADODB.Stream")
Stream.Open()
Stream.Type = 1
Stream.LoadFromFile(FilePath)
Response.BinaryWrite Stream.Read()
Stream.Close
Set Stream = Nothing
Response.End
%>

(I really wish we could edit posts here, I hate having to repeat myself if I make a typo the first time round...)
0
 
JakobACommented:
Only now those files will never open in the browser, not even for those users who have set their browser to automatically load those filetypes.
0
 
ZontarCommented:
The original request was for a *download* link, not an "open" link, mate. :^)
0
 
ZontarCommented:
Actually, I meant to say, "a *download* button, not an "open" button" but you get the idea, hopefully <G>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now