文件导入
<el-upload class="upload-demo" style="display:inline" :show-file-list="false" action="#" accept="application/json" :before-upload="eventfile"> <a-button class="header-option-button" size="small" icon="cloud-upload"></a-button> </el-upload> methods:{ // 上传json文件 improtfile(data) { return new Promise( function(resolve, reject) { const reader = new FileReader() reader.readAsText(data) reader.onload = function(e) { const str = e.target.result resolve(str) } } ) },async eventfile(file) { const res = JSON.parse(await this.improtfile(file)); console.log(res) return false; }, }文件导出
<a-button class="header-option-button" size="small" icon="cloud-download" @click="downloadfile" download></a-button> methods:{ downLoad(data, filename) { if (!data) { alert('导出的数据为空'); return; } if (!filename) filename = 'json.json' if (typeof data === 'object') { data = JSON.stringify(data, undefined, 4) } var blob = new Blob([data], { type: 'text/json' }), e = document.createEvent('MouseEvents'), a = document.createElement('a') a.download = filename a.href = window.URL.createObjectURL(blob) a.dataset.downloadurl = ['text/json', a.download, a.href].join(':') e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) a.dispatchEvent(e) }, // 导出json文件 downloadfile() { this.downLoad(JSON.stringify(this.formdata), this.formdata + ".文件后缀名"); }, }