Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Save Files Locally from Server IE only

    The simple duo of functions startSaveText and saveText will let your users save
    selected text/html to their machine with a simple click.

    The use of execCommand makes this script only for those running IE intranets.
    IE allows you to save server files locally through script with execCommand.

    This script requires the Props helper function. Props can be found here:
    Read Local Text Files, Quick Set Properties, and Custom Error Handler

    You can remove the Props call, but remember to hide the iframe.


    The mechanics are:
    open a file on the server in a new iframe/window and then save the content locally to the user.
    A new window can be used instead of an iframe, but you will need to address popup issues.

    Code:
    /********************************************************
     * startSave builds and loads the iframe that will 
     * deliver the server content
     * IN: source = the path of the source file on the server
     *     dest = the destination file name for the client
     * OUT: no return
     *     initiates an iframe onload callback to saveText
     ********************************************************/
    function startSaveText(source,dest){
    	var div = document.createElement("div");
    	Props(div.style,{position:"absolute",height:"20px",top:"-200px"})	//quick set style properties
    	document.body.appendChild(div);
    	var s = "<iframe src='"+source+"' onload='saveText(this.contentWindow.document,\""+dest+"\");this.parentNode.parentNode.removeChild(this.parentNode);'></iframe>";
    	div.innerHTML=s;
    }
    
    /********************************************************
     * saveText saves the text to the clients file system
     * IN: obj = the document object of an iframe/window
     *     dest = the destination file name for the client
     * OUT: no return
     *     opens a file save dialog on the client system
     ********************************************************/
    function saveText(obj,dest){
    	obj.execCommand("SaveAs", false, dest);
    }


    A simple demonstration use is:
    The first link offers the client user the option to save the server file 'file1.txt' locally under the suggested name 'newFile1.txt'
    The second link offers the client an option to save the server file 'file2.txt' lcally under the suggested name 'newFile2.html'
    Code:
    <a href="http://notreal.com" onclick="startSaveText('file1.txt','newFile1.txt');return false;">Save Your File!</a>
    <a href="http://notreal.com" onclick="startSaveText('file2.txt','newFile2.html');return false;">Save Another File!</a>


    The 2 files in the example coud be plain text, or html files, etc.

    You may need to address PRE wrap and html entity issues depending on the content.

    provided by rdspoons
    Last edited by rdspoons; 08-24-2010 at 03:25 AM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •