-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Xuehui Ye
committed
Nov 23, 2019
1 parent
4047fc4
commit cf3f919
Showing
2 changed files
with
62 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,65 @@ | ||
function download(filename, text) { | ||
var element = document.createElement('a'); | ||
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | ||
element.setAttribute('download', filename); | ||
import { tilesInBBox } from "../quadtile"; | ||
|
||
element.style.display = 'none'; | ||
document.body.appendChild(element); | ||
/** | ||
* 文件类 | ||
*/ | ||
class Mfile { | ||
static downloadText(filename, text) { | ||
var element = document.createElement('a'); | ||
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); | ||
element.setAttribute('download', filename); | ||
|
||
element.click(); | ||
element.style.display = 'none'; | ||
document.body.appendChild(element); | ||
|
||
document.body.removeChild(element); | ||
element.click(); | ||
|
||
document.body.removeChild(element); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*Sting} rows | ||
*/ | ||
static rows2csv(rows) { | ||
var csvFile = rows.reduce(function (finalVal, row) { | ||
for (var j = 0; j < row.length; j++) { | ||
var innerValue = row[j] === null ? '' : row[j].toString(); | ||
if (row[j] instanceof Date) { | ||
innerValue = row[j].toLocaleString(); | ||
}; | ||
var result = innerValue.replace(/"/g, '""'); | ||
if (result.search(/("|,|\n)/g) >= 0) | ||
result = '"' + result + '"'; | ||
if (j > 0) | ||
finalVal += ','; | ||
finalVal += result; | ||
} | ||
return finalVal + '\n'; | ||
}, '') | ||
return csvFile; | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} filename | ||
* @param {*} rows | ||
* @param {Array} titles 提供标题栏 | ||
*/ | ||
static downloadRows(filename, rows, titles = null) { | ||
if (rows.length > 0) { | ||
if (!(rows[0] instanceof Array)) { | ||
var newRows = titles ? [titles] : [Object.keys(rows[0])]; | ||
rows.map((row) => { | ||
newRows.push(Object.values(row)) | ||
}) | ||
rows = newRows | ||
} else if (titles) { | ||
rows.unshift(titles) | ||
} | ||
this.downloadText(filename, this.rows2csv(rows)) | ||
} | ||
} | ||
} | ||
|
||
// Start file download. | ||
download("hello.txt","This is the content of my file :)"); | ||
export default Mfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters