Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

save to text or html file very good

حفظ البيانات في ملف نصي او صفحة ويب مميز جدا//
const downloadToFile = (content, filename, contentType) => {
  const a = document.createElement('a');
  const file = new Blob([content], {type: contentType});
  
  a.href= URL.createObjectURL(file);
  a.download = filename;
  a.click();

    URL.revokeObjectURL(a.href);
};

document.querySelector('#btnSave').addEventListener('click', () => {
  const textArea = document.querySelector('textarea');
  
  downloadToFile(textArea.value, 'my-new-file.txt', 'text/plain');
});

// 222 ---------------
function gateFile() {
const fileE = document.getElementById("fileE").value;

// (A) CREATE BLOB OBJECT
var myBlob = new Blob([fileE], {type: "html/text/plain"});

// (B) CREATE DOWNLOAD LINK
var url = window.URL.createObjectURL(myBlob);
var anchor = document.createElement("a");
anchor.href = url;
anchor.download = "demo.html";
    
// (C) "FORCE DOWNLOAD"
// NOTE: MAY NOT ALWAYS WORK DUE TO BROWSER SECURITY
// BETTER TO LET USERS CLICK ON THEIR OWN
anchor.click();
window.URL.revokeObjectURL(url);
document.removeChild(anchor);

}
//--------------33
const texE=document.getElementById("codeThreeE");

  let blob = new Blob([texE.value], {type: 'text/plain'});
      link.href = URL.createObjectURL(blob); 
      link.download="save way3.text"; 

//--------------44
// btn type link
      function fourway() {

    const codeFourE = document.getElementById("codeFourE");
             
      let link = document.createElement('a');
      link.download = 'save way4.txt';
      let blob = new Blob([codeFourE.value], {type: 'text/plain'});
      link.href = URL.createObjectURL(blob);
      link.click();
      URL.revokeObjectURL(link.href);
         }
//--------------55
     function fiveway() {
      
      const codeFiveE = document.getElementById("codeFiveE");

      let link = document.createElement('a');
      link.download = 'save way5.txt';
      let blob = new Blob([codeFiveE.value], {type: 'text/plain'});
      let reader = new FileReader();
      reader.readAsDataURL(blob); // converts ta blob to base64 and calls onload
      reader.onload = function() {
        link.href = reader.result; // data url
        link.click();
      }; 
           };
//sorce 2 
//1) https://robkendal.co.uk/blog/2020-04-17-saving-text-to-client-side-file-using-vanilla-js
//2) https://code-boxx.com/create-save-files-javascript/
Comment

PREVIOUS NEXT
Code Example
Javascript :: prisma multiple relation counts 
Javascript :: react-native navigation homeStack 
Javascript :: CUSTOM_ELEMENTS_SCHEMA 
Javascript :: Toggle image onclicking parent 
Javascript :: Hardhat deploy sample js code 
Javascript :: es6 javascript return types 
Javascript :: Looping through array, fetching tweets and returning new reversed array javascript react 
Javascript :: jQuery mobile anchor link on the same page 
Javascript :: how to check bot channel permissions 
Javascript :: check for overlapping time javascript 
Javascript :: get images from mysql with php jquery ajax and display them in html page inside DIVs 
Javascript :: simple express server responce html css js 
Javascript :: get data from multiple api in react 
Javascript :: discord.js create a private channel 
Javascript :: auto load window on change viewport react 
Javascript :: chart cdn js 
Javascript :: how to run json server 
Javascript :: js onclick add table row 
Javascript :: convert milliseconds to dd/mm/yyyy javascript 
Javascript :: mounting in react 
Javascript :: how to validate multiple input field in javascript 
Javascript :: multiple path names for a same component in react router v6 
Javascript :: loop backwards javascript 
Javascript :: Manage selection fabric js 
Javascript :: arjs marker+location 
Javascript :: JavaScript Rules for Naming JavaScript Variables 
Javascript :: javascript Symbol Properties 
Javascript :: javascript "use strict" 
Javascript :: find the missing number in js 
Javascript :: promise limit time 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =