您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何實現基于React-Dropzone開發上傳組件功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1. axios上傳普通文件:
通過yarn將react-dropzone和引入:
yarn add react-dropzone axios
前端js如下(如有缺失,請自行修改):
import React, { useState, useCallback, useEffect, } from 'react'; import {useDropzone} from 'react-dropzone'; import "./dropzone.styles.css" import InfiniteScroll from 'react-infinite-scroller'; import { List, message, // Avatar, Spin, } from 'antd'; import axios from 'axios'; /** * 計算文件大小 * @param {*} bytes * @param {*} decimals * @returns */ function formatBytes(bytes, decimals = 2) { if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } /** * Dropzone 上傳文件 * @param {*} props * @returns */ function DropzoneUpload(props) { const [files, setFiles] = useState([]) const [loading, setLoading] = useState(false); const [hasMore, setHasMore] = useState(true); const onDrop = useCallback(acceptedFiles => { setLoading(true); const formData = new FormData(); smallFiles.forEach(file => { formData.append("files", file); }); axios({ method: 'POST', url: '/api/files/multiplefiles', data: formData, headers: { "Content-Type": "multipart/form-data", } }) then(resp => { addFiles(acceptedFiles); setLoading(false); }); }, [files]); // Dropzone setting const { getRootProps, getInputProps } = useDropzone({ multiple:true, onDrop, }); // 刪除附件 const removeFile = file => { const newFiles = [...files] newFiles.splice(newFiles.indexOf(file), 1) setFiles(newFiles) } useEffect(() => { // init uploader files setFiles([]) },[]) return ( <section className="container"> <div {...getRootProps({className: 'dropzone'})}> <input {...getInputProps()} /> <p>拖動文件或點擊選擇文件?</p> </div> <div className="demo-infinite-container"> <InfiniteScroll initialLoad={false} pageStart={0} loadMore={handleInfiniteOnLoad} hasMore={!loading && hasMore} useWindow= {false} > <List dataSource={files} renderItem={item=> ( <List.Item actions={[ // <a key="list-loadmore-edit">編輯</a>, <a key="list-loadmore-delete" onClick={removeFile}>刪除</a> ]} // extra={ // } key={item.path}> <List.Item.Meta avatar={ <> { !!item.type && ['image/gif', 'image/jpeg', 'image/png'].includes(item.type) && <img width={100} alt='logo' src={item.preview} /> } </> } title={item.path} description={formatBytes(item.size)} /> </List.Item> )} > {loading && hasMore && ( <div className="demo-loading-container"> <Spin /> </div> )} </List> </InfiniteScroll> </div> </section> ); }
flask代碼:
def multiplefiles(): if 'files' not in request.files: return jsonify({'message': '沒有文件!'}), 200 files = request.files.getlist('files') for file in files: if file: # 通過拼音解決secure_filename中文問題 filename = secure_filename(''.join(lazy_pinyin(file.filename)) Path(UPLOAD_FOLDER + '/' + file_info['dir_path']).mkdir(parents=True, exist_ok=True) file.save(os.path.join(UPLOAD_FOLDER + '/' + file_info['dir_path'], filename)) return jsonify({'message': '保存成功!!'})
2. 大文件導入:
通過file.slice()方法生成文件的chunks。不要用Promise.all容易產生非順序型的請求,導致文件損壞。
js代碼:
const promiseArray = largeFiles.map(file => new Promise((resolve, reject) => { const chunkSize = CHUNK_SIZE; const chunks = Math.ceil(file.size / chunkSize); let chunk = 0; let chunkArray = new Array(); while (chunk <= chunks) { let offset = chunk * chunkSize; let slice = file.slice(offset, offset+chunkSize) chunkArray.push([slice, offset]) ++chunk; } const chunkUploadPromises = (slice, offset) => { const largeFileData = new FormData(); largeFileData.append('largeFileData', slice) return new Promise((resolve, reject) => { axios({ method: 'POST', url: '/api/files/largefile', data: largeFileData, headers: { "Content-Type": "multipart/form-data" } }) .then(resp => { console.log(resp); resolve(resp); }) .catch(err => { reject(err); }) }) }; chunkArray.reduce( (previousPromise, [nextChunk, nextOffset]) => { return previousPromise.then(() => { return chunkUploadPromises(nextChunk, nextOffset); }); }, Promise.resolve()); resolve(); }))
flask代碼:
filename = secure_filename(''.join(lazy_pinyin(filename))) Path(UPLOAD_FOLDER + '/' + file_info['dir_path']).mkdir(parents=True, exist_ok=True) save_path = os.path.join(UPLOAD_FOLDER + '/' + file_info['dir_path'], filename) try: with open(save_path, 'ab') as f: f.seek(offset) f.write(file.stream.read()) print("time: "+ str(datetime.now())+" offset: " + str(offset)) except OSError: return jsonify({'Could not write to file'}), 500
關于“如何實現基于React-Dropzone開發上傳組件功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。