General Method distal download file (compatible IE)

Before the Internet to see a blogger to write front-end common method of downloading files, personal feel very practical, so mark about the future for easy access

Address of the source text (source text as well as upload / download excel file method)

Because the project required to be compatible with IE browsers, so perfect for a moment before the method blogger

  • IE browser: Use Microsoft's own msSaveBlob method, download attribute a label that does not support IE
  • Google browser (Google only tested): create a tag, add the download properties to simulate a mouse click events

  

 

// Here res.data the blob is returned 
        var blob = new new Blob ([res.data.fileData], {type: 'file application / JSON; charset = UTF-. 8'}); // file application / vnd.openxmlformats- officedocument.wordprocessingml.document herein denotes doc type 
        var the href = window.URL.createObjectURL (BLOB); // create a download link 
        IF (window.navigator.msSaveBlob) {
           the try { 
            window.navigator.msSaveBlob (BLOB, 'model.json ' ) 
          } the catch (E) { 
            the console.log (E); 
          } 
        } the else {
           // Google browser to create a tag attribute added download downloads
          var downloadElement = document.createElement ( 'A' ); 
          downloadElement.href = the href; 
          downloadElement.target = '_blank' ; 
          downloadElement.download = 'model.json'; // After downloading the file name 
          document.body.appendChild (downloadElement) ; 
          downloadElement.click (); // Download 
          document.body.removeChild (downloadElement); // Download the complete removal of elements 
          window.URL.revokeObjectURL (href); // freed the blob 
        }

 

Guess you like

Origin www.cnblogs.com/mmzuo-798/p/11200141.html