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?
econy1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.