Skip to content

Commit

Permalink
feat(utils): 完善 downloadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsn committed May 10, 2023
1 parent d32dac2 commit 7d6f425
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
{ title: 'Hooks', link: '/hooks/useClickAway' },
{ title: '转换', link: '/formats/addThousandSeparator' },
{ title: '校验', link: '/validators/isAsyncFunction' },
{ title: '工具', link: '/utils/downloadFile' },
],
footer: `Copyright © ${new Date().getFullYear()} nmsn`,
logo: '/logo.png',
Expand All @@ -20,6 +21,7 @@ export default defineConfig({
{ type: 'hooks', dir: '/src/hooks' },
{ type: 'format', dir: 'src/format' },
{ type: 'validator', dir: 'src/validator' },
{ type: 'utils', dir: 'src/utils' },
],
codeBlockMode: 'passive',
forceKebabCaseRouting: false,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './format';
export * from './hooks';
export * from './regex';
export * from './types';
export * from './utils';
export * from './validator';
14 changes: 14 additions & 0 deletions src/utils/downloadFile/demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

import { downloadFile } from '@nmsn/utils';

const url = 'https://s2.loli.net/2023/04/20/4t8NgpMxkdVCJcs.png';

export default () => {
return (
<>
<img src={url} alt="logo" style={{ width: 200, height: 150 }} />
<button onClick={() => downloadFile(url)}>click</button>
</>
);
};
23 changes: 23 additions & 0 deletions src/utils/downloadFile/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: downloadFile
toc: false
---

## downloadFile

下载文件,避免浏览器默认行为,如图片pdf会被浏览器直接打开

<code src="./demo.tsx"></code>

### API

```typescript
downloadFile(url: string, name?: string);
```

### Params

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ---- | -------- | ------ |
| url | 资源地址 | `string` | - |
| name | 覆盖的文件名 | `string` | - |
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** 下载文件 */
const loadFile = (fileUrl: string, name?: string) => {
const downloadFile = (fileUrl: string, name?: string) => {
fetch(fileUrl)
.then(res => res.blob())
.then(blob => {
Expand All @@ -19,4 +19,4 @@ const loadFile = (fileUrl: string, name?: string) => {
});
};

export default loadFile;
export default downloadFile;
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as download } from './download';
export { default as downloadFile } from './downloadFile';

1 comment on commit 7d6f425

@vercel
Copy link

@vercel vercel bot commented on 7d6f425 May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

utils – ./

utils-git-main-nmsn.vercel.app
nmsn-utils.vercel.app
utils-nmsn.vercel.app

Please sign in to comment.