javascript - Download a text file from textarea on Button Click - Stack Overflow

I am using Jquery UI Dialog. Within the dialog there is textarea that have some text. And I need to sav

I am using Jquery UI Dialog. Within the dialog there is textarea that have some text. And I need to save that text as textfile like data.txt when I click the button in the dialog.

<div id = 'metaDataDialog' title='Meta Data' >
  <textarea id = 'metaText'>
     Some Text
  </textarea>
</div>

and this is the jquery ui dialog

$("#metaDataDialog").dialog({ //Jquery UI Dialog Intialization
           autoOpen: false, 
           modal: true, 
           width: 400,
           height: 300, 
           buttons: {
              Save: function() {},                   
              Cancel: function() { $(this).dialog( "close" ); }
           },                  
        });     

and I need to save/download the text in the local machine, when the save button is clicked

I am using Jquery UI Dialog. Within the dialog there is textarea that have some text. And I need to save that text as textfile like data.txt when I click the button in the dialog.

<div id = 'metaDataDialog' title='Meta Data' >
  <textarea id = 'metaText'>
     Some Text
  </textarea>
</div>

and this is the jquery ui dialog

$("#metaDataDialog").dialog({ //Jquery UI Dialog Intialization
           autoOpen: false, 
           modal: true, 
           width: 400,
           height: 300, 
           buttons: {
              Save: function() {},                   
              Cancel: function() { $(this).dialog( "close" ); }
           },                  
        });     

and I need to save/download the text in the local machine, when the save button is clicked

Share Improve this question edited Feb 2, 2016 at 8:45 Prem asked Feb 2, 2016 at 8:40 PremPrem 6522 gold badges12 silver badges25 bronze badges 5
  • Do you want data.txt to end up on your webserver or on your local machine that is running the browser? – Marijn van Vliet Commented Feb 2, 2016 at 8:43
  • @Rodin in the local machine – Prem Commented Feb 2, 2016 at 8:44
  • 1 check this link stackoverflow./questions/27398074/… – guradio Commented Feb 2, 2016 at 8:47
  • @guradio it wont work, they are using anchor tag. jQuery UI dialog buttons cannot have anchor tags – Prem Commented Feb 2, 2016 at 8:49
  • 2 then use a different one. it is just an idea on how to make text file you can make it. – guradio Commented Feb 2, 2016 at 8:52
Add a ment  | 

1 Answer 1

Reset to default 8
<html>
<head>
<script src="http://code.jquery./jquery-1.10.2.js"></script>


<script>

    $(document).ready(function () {

        function saveTextAsFile() {
            // grab the content of the form field and place it into a variable
            var textToWrite = document.getElementById("content").value;
            //  create a new Blob (html5 magic) that conatins the data from your form feild
            var textFileAsBlob = new Blob([textToWrite], { type: 'text/plain' });
            // Specify the name of the file to be saved
            var fileNameToSaveAs = "myNewFile.txt";

            // Optionally allow the user to choose a file name by providing 
            // an imput field in the HTML and using the collected data here
            // var fileNameToSaveAs = txtFileName.text;

            // create a link for our script to 'click'
            var downloadLink = document.createElement("a");
            //  supply the name of the file (from the var above).
            // you could create the name here but using a var
            // allows more flexability later.
            downloadLink.download = fileNameToSaveAs;
            // provide text for the link. This will be hidden so you
            // can actually use anything you want.
            downloadLink.innerHTML = "My Hidden Link";

            // allow our code to work in webkit & Gecko based browsers
            // without the need for a if / else block.
            window.URL = window.URL || window.webkitURL;

            // Create the link Object.
            downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
            // when link is clicked call a function to remove it from
            // the DOM in case user wants to save a second file.
            downloadLink.onclick = destroyClickedElement;
            // make sure the link is hidden.
            downloadLink.style.display = "none";
            // add the link to the DOM
            document.body.appendChild(downloadLink);

            // click the new link
            downloadLink.click();
        }

        function destroyClickedElement(event) {
            // remove the link from the DOM
            document.body.removeChild(event.target);
        }



        $("#download").click(function (e) {

            e.preventDefault();
            saveTextAsFile();
        });
 });  
</script>
</head>
<body>
    <input type="button" id="download" value="Download" />
   <textarea id="content">In trying to keep this plugin as simple as possible, all four states are always assumed to be present. You should prepare your button image as a single image the width you want your button, and four times the height of the button. All four states should then live in that one image in the same order as the previous list from top to bottom.</textarea>


</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312125a4420103.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信