Skip to content

Commit d1aa785

Browse files
author
ChenYong
committed
expose downloadAsFile; add api options in uploading; alpha release
1 parent 2402c3e commit d1aa785

File tree

7 files changed

+17
-9
lines changed

7 files changed

+17
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ configureUploading({ apiHost: "https://demo-host.com", locales: {} });
2020
uploadByUrl;
2121
getDownloadUrl;
2222
getUploadUrl;
23+
downloadAsFile;
2324
```
2425

2526
当前模块提供了四个组件:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jimengio/files-picker",
3-
"version": "0.0.1-a11",
3+
"version": "0.0.1-a12",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

src/api.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { get, put, post } from "@jimengio/api-base";
1+
import { get, put, post, IJimuApiOption } from "@jimengio/api-base";
22
import { IUpload, IDownload } from "./model";
33

44
import { apiHost } from "./config";
@@ -13,13 +13,14 @@ export const uploadSign = async (url: string, fileName: string) => {
1313
});
1414
};
1515

16-
export const uploadByUrl = async (fileUrl: string, file: File) => {
16+
export const uploadByUrl = async (fileUrl: string, file: File, options?: IJimuApiOption) => {
1717
return put<void>({
1818
url: fileUrl,
1919
data: file,
2020
headers: {
2121
"Content-Type": mime.contentType(file.type),
2222
},
23+
...options,
2324
});
2425
};
2526

src/component/base-display.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { css } from "emotion";
44
import { Icon } from "antd";
55
import { Space } from "@jimengio/flex-styles";
66
import { useDownloadApi } from "../hooks";
7-
import { download } from "../util";
7+
import { downloadAsFile } from "../util";
88

99
interface IProps {
1010
url: string;
@@ -34,7 +34,7 @@ let BaseDisplay: FC<IProps> = React.memo((props) => {
3434
className={style.mouse}
3535
onClick={async () => {
3636
const downloadUrl = await downloadingResource.startDownload(props.url, path);
37-
download(downloadUrl);
37+
downloadAsFile(downloadUrl);
3838
}}
3939
/>
4040
{readOnly ? null : (

src/component/dropzone.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import JimoIcon, { EJimoIcon } from "@jimengio/jimo-icons";
99
import { interpolateLocale } from "../util";
1010
import { uploadingLocales } from "../config";
1111
import { useUploadApi, useDownloadApi } from "../hooks";
12-
import { download } from "../util";
12+
import { downloadAsFile } from "../util";
1313

1414
interface IProps {
1515
className?: string;
@@ -139,7 +139,7 @@ let Dropzone: FC<IProps> = React.memo((props) => {
139139
let reqDownload = async (path: string) => {
140140
try {
141141
const downloadUrl = await downloadingResource.startDownload(props.downloadUrl, path);
142-
download(downloadUrl);
142+
downloadAsFile(downloadUrl);
143143
} catch (error) {
144144
console.error(error);
145145
message.error(uploadingLocales.downloadFailure);

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export { default as Dropzone } from "./component/dropzone";
66
export { uploadByUrl, getDownloadUrl, uploadSign } from "./api";
77
export { useDownloadApi, useUploadApi } from "./hooks";
88
export { processImageUrl } from "./util/image";
9+
10+
export { downloadAsFile } from "./util";

src/util.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export const download = (url: string, fileName?: string) => {
1+
/**
2+
* Use this paired with Content-Disposition
3+
* https://stackoverflow.com/a/9195376/883571
4+
*/
5+
export const downloadAsFile = (url: string, fileName?: string) => {
26
const a = document.createElement("a");
37
a.href = url;
48
a.setAttribute("download", "true");
@@ -19,7 +23,7 @@ export const getAttachmentName = (attach: string): string => {
1923

2024
export const downloadByBlob = (data: Blob, fileName?: string) => {
2125
const url = URL.createObjectURL(data);
22-
download(url, fileName);
26+
downloadAsFile(url, fileName);
2327
};
2428

2529
export function interpolateLocale(template: string, data: { [k: string]: any }) {

0 commit comments

Comments
 (0)