Showing posts with label Save Page data in to html and download using javascript. Show all posts
Showing posts with label Save Page data in to html and download using javascript. Show all posts

Thursday, 1 May 2014

Save Page data in to html and download using javascript


<script type="text/javascript">
  
    var showSave;
    var DownloadAttributeSupport = 'download' in document.createElement('a');
    navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob;
    var BrowserSupportedMimeTypes = {
        "image/jpeg": true,
        "image/png": true,
        "image/gif": true,
        "image/svg+xml": true,
        "image/bmp": true,
        "image/x-windows-bmp": true,
        "image/webp": true,
        "audio/wav": true,
        "audio/mpeg": true,
        "audio/webm": true,
        "audio/ogg": true,
        "video/mpeg": true,
        "video/webm": true,
        "video/ogg": true,
        "text/plain": true,
        "text/html": true,
        "text/xml": true,
        "application/xhtml+xml": true,
        "application/json": true
    };
    if (Blob && navigator.saveBlob) {
        showSave = function (data, name, mimetype) {
            // var builder = new Blob(data);
            var blob = new Blob([data], { type: 'text/css' });
            //builder.append(data);
           // var blob = builder.getBlob(mimetype || "application/octet-stream");
            if (!name) name = "Download.bin";
            if (window.saveAs) {
                window.saveAs(blob, name);
            }
            else {
                navigator.saveBlob(blob, name);
            }
        };
    }
    else if (Blob && URL) {
        showSave = function (data, name, mimetype) {
            //var blob, url, builder = new Blob(data);
            var url, blob = new Blob([data], { type: 'text/css' });
           
            //builder.append(data);
            if (!mimetype) mimetype = "application/octet-stream";
            if (DownloadAttributeSupport) {
               // blob = builder.getBlob(mimetype);
                url = URL.createObjectURL(blob);
                var link = document.createElement("a");
                link.setAttribute("href",url);
                link.setAttribute("download",name||"Download.bin");
                var event = document.createEvent('MouseEvents');
                event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                link.dispatchEvent(event);
            }
            else {
                if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) {
                    mimetype = "application/octet-stream";
                }

                //blob = builder.getBlob(mimetype);
                url = URL.createObjectURL(blob);
                window.open(url, '_blank', '');
            }
            setTimeout(function () {
                URL.revokeObjectURL(url);
            }, 250);
        };
    }
    else if (!/\bMSIE\b/.test(navigator.userAgent)) {
        showSave = function (data, name, mimetype) {
            if (!mimetype) mimetype = "application/octet-stream";
            // Again I need to filter the mime type so a download is forced.
            if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) {
                mimetype = "application/octet-stream";
            }
            window.open("data:" + mimetype + "," + encodeURIComponent(data), '_blank', '');
        };
    }
    function saveData() {
        if (!showSave) {
            alert("Your browser does not support any method of saving JavaScript gnerated data to files.");
            return;
        }

        showSave(
         document.getElementById("data").value,
         document.getElementById("filename").value,
         document.getElementById("mimetype").value);
    }
  
    </script>
</head>
<body>
<label for="filename">File name:</label>
<input type="text" id="filename" value="Example.html"/>

<label for="mimetype">Mime type:</label>
<input type="text" id="mimetype" value="text/plain; charset=UTF-8"/>



<label for="data">File contents:</label>

<textarea id="data">Multiline text containing unicode characters.

Welcome...

</textarea>



<button onclick="saveData();">Save Data</button>