forked from RobinYang11/goji
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d0872e3
commit fc311be
Showing
5 changed files
with
121 additions
and
61 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
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,3 +1,10 @@ | ||
.upload { | ||
color: red; | ||
position: relative; | ||
cursor: pointer; | ||
|
||
input { | ||
position: absolute; | ||
opacity: 0; | ||
width: 200px; | ||
} | ||
} |
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
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,18 +1,61 @@ | ||
import React from 'react' | ||
import React, { useRef } from 'react' | ||
import styles from './upload.module.less'; | ||
|
||
interface IUploadProps { | ||
url: string; | ||
name: string, | ||
onClick(): void | ||
uploadUrl: string; | ||
children: React.ReactNode; | ||
onComplete?(data: unknown): void; | ||
} | ||
|
||
export default function Upload(props: IUploadProps) { | ||
const { onClick, url, name } = props | ||
return <div | ||
title={name} | ||
onClick={onClick} | ||
className={styles.upload}> | ||
{name} | ||
</div> | ||
|
||
const { children, uploadUrl, onComplete } = props | ||
const inputRef = useRef<any>(); | ||
const uploadFile = (files: FileList) => { | ||
const promiese = [] | ||
for (let i = 0; i < files.length; i++) { | ||
const p = new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
const formData = new FormData(); | ||
formData.append('file', files[0]) | ||
xhr.open('POST', uploadUrl) | ||
xhr.send(formData) | ||
xhr.onerror = err => { | ||
reject(err) | ||
} | ||
xhr.onload = () => { | ||
let result; | ||
try { | ||
result = JSON.parse(xhr.response) | ||
} catch (e) { | ||
result = xhr.response; | ||
} | ||
resolve(result) | ||
} | ||
}); | ||
promiese.push(p) | ||
} | ||
Promise.all(promiese).then(res => { | ||
onComplete?.(res) | ||
}) | ||
} | ||
|
||
return <span | ||
onClick={() => { | ||
inputRef.current.click(); | ||
}} | ||
className={styles.upload} | ||
> | ||
{children} | ||
<input | ||
multiple | ||
onChange={e => { | ||
if (e.target.files) { | ||
uploadFile(e.target.files) | ||
} | ||
}} | ||
ref={inputRef} | ||
type="file" | ||
/> | ||
</span> | ||
} |
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