reading the contents of blob in javascript

我的未来我决定 提交于 2020-01-25 00:02:24

问题


Finally I succeeded in creating an xml file locally & downloading it on a pc using JavaScript & BLOB in HTML 5. (as shown in code below).

This file can be stored as a text or as xml type. now my question is how to open this file (again from local disk in PC) using JavaScript & load the values of a particular node to a respective field in HTML form. ??

can anyone please help me with this ?

// to save the contents :

  function savedata() {

            var feederdocument = document.createElement("FeederDocument");
            var feederreport = document.createElement("FeederReport");
            if (document.getElementById("feederserialnumber").value == '') {
                alert('Please Enter the Serial Number')
            }
            else {

                var fileextns = '.xml';

                if (fileextns == '.xml') {
                    var xmlString = '<?xml version="1.0" encoding="utf-8" ?>';
                }
                else {
                    var xmlString = '';
                }

                var orderdata = document.createElement("OrderData");

                orderdata.appendChild(document.createElement("ClientName"));


                orderdata.appendChild(document.createElement("ContactPerson"));
                orderdata.appendChild(document.createElement("Country"));
                orderdata.appendChild(document.createElement("Designation"));
                orderdata.appendChild(document.createElement("Reference"));
                orderdata.appendChild(document.createElement("Department"));
                orderdata.appendChild(document.createElement("OrderNumber"));
                orderdata.appendChild(document.createElement("PhoneNumber"));
                orderdata.appendChild(document.createElement("SpecialistName"));
                orderdata.appendChild(document.createElement("MobileNumber"));
                orderdata.appendChild(document.createElement("Duration"));
                orderdata.appendChild(document.createElement("Emailid"));

                feederreport.appendChild(orderdata);
                feederdocument.appendChild(feederreport);

// For Downloading the contents in xml format; I'm using blob as shown :

var res = xmlString.concat(feederdocument.innerHTML);
            var textFileAsBlob = new Blob([res], { type: 'text/html' });
            var fileNameToSaveAs = (document.getElementById("feederserialnumber").value).concat(fileextns);

            var downloadLink = document.createElement("a");
            downloadLink.download = fileNameToSaveAs;
            downloadLink.innerHTML = "Download File";
            if (window.webkitURL != null) {

                downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
            }
            else {

                downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
                downloadLink.onclick = destroyClickedElement;
                downloadLink.style.display = "none";
                document.body.appendChild(downloadLink);
            }

            downloadLink.click();
        }
        function destroyClickedElement(event) {
            document.body.removeChild(event.target);
        }

来源:https://stackoverflow.com/questions/32554144/reading-the-contents-of-blob-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!