window.open text格式直接打开而不是下载

针对文本格式,会直接打开,如果想下载,可以使用以下代码:

content为text内容,可以从后台传来,前端声明等等,fileName为文件名,主要是将txt封装成a标签进行下载主要是将txt封装成a标签进行下载

更多资源访问

  download(content) {
      let blob = new Blob([content], {
        type: 'application/octet-stream',
      })
      let href = window.URL.createObjectURL(blob)
      // 兼容 ie
      if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(blob, i.name)
      } else {
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = href
        link.download = decodeURIComponent(this.resourceName + '.txt')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(link)
      }
    },

你可能感兴趣的:(前端,javascript,html)