Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(n-image): download method support local file #6396

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

`2024-09-26`

### Features

- `n-image` `download` support method local file, close [#6394](https://github.com/tusen-ai/naive-ui/issues/6394)

### Fixes

- Fix `n-data-table` in virtual-x mode, when all column objects do not have the `fixed` attribute configured, it cannot be displayed normally.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

`2024-09-26`

### Features

- `n-image` `download` 方法支持本地文件,关闭 [#6394](https://github.com/tusen-ai/naive-ui/issues/6394)

### Fixes

- 修复 `n-data-table` 在设定 `virtual-x`,且所有的 column 对象均没有配置 `fixed` 属性的时候无法正常显示
Expand Down
29 changes: 26 additions & 3 deletions src/_utils/dom/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,30 @@ export function download(url: string | null, name: string | undefined): void {
if (name !== undefined) {
a.download = name
}
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
else {
const urlWithoutQuery = url.split('?')[0]
const parts = urlWithoutQuery.split('/')
const filename = parts[parts.length - 1]
a.download = filename
}
if (url.startsWith('http') || url.startsWith('blob')) {
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
else {
fetch(url)
.then(res => res.blob())
.then((blob) => {
const objectUrl = URL.createObjectURL(blob)
a.href = objectUrl
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(objectUrl)
})
.catch((err) => {
console.error('Error fetching file:', err)
})
}
}
Loading