乐闻世界logo
搜索文章和话题

How to use react-query useMutation with a multi-part file upload?

1个答案

1

The steps to implement chunked file upload using useMutation in React Query are as follows:

1. Prepare File Chunks

First, split the file into smaller chunks. This is commonly achieved by processing the file object in the frontend. You can define a function to split the file:

javascript
function splitFile(file, chunkSize) { let chunks = []; let count = Math.ceil(file.size / chunkSize); for (let i = 0; i < count; i++) { let chunk = file.slice(chunkSize * i, Math.min(file.size, chunkSize * (i + 1))); chunks.push(chunk); } return chunks; }

2. Define Upload Function

Define a function to handle the upload of individual chunks. This function will be invoked by useMutation:

javascript
async function uploadChunk(chunk, index, fileId) { const formData = new FormData(); formData.append("file", chunk); formData.append("index", index); formData.append("fileId", fileId); const response = await fetch("https://your-api.com/upload", { method: "POST", body: formData, }); return response.json(); }

3. Use useMutation for Management

Use useMutation in your component to manage the file upload process, including error handling and state updates:

javascript
import { useMutation } from 'react-query'; function FileUploader({ file }) { const mutation = useMutation(({ chunk, index, fileId }) => uploadChunk(chunk, index, fileId), { onSuccess: (data) => { console.log('Chunk uploaded successfully:', data); }, onError: (error) => { console.error('Failed to upload chunk:', error); }, }); const handleUpload = async () => { const chunkSize = 1024 * 1024; // 1MB const chunks = splitFile(file, chunkSize); const fileId = generateFileId(); // Assume this function generates a unique file ID chunks.forEach((chunk, index) => { mutation.mutate({ chunk, index, fileId }); }); }; return ( <div> <button onClick={handleUpload} disabled={mutation.isLoading}> {mutation.isLoading ? 'Uploading...' : 'Upload File'} </button> {mutation.isError && <p>Error uploading file</p>} {mutation.isSuccess && <p>File uploaded successfully</p>} </div> ); }

4. Error Handling and Retry Strategy

You can configure retry strategies within useMutation, for example, by specifying the number of retries or retrying based on certain conditions:

javascript
const mutation = useMutation(uploadChunk, { retry: (failureCount, error) => error.status < 500 && failureCount < 3, });

Summary

By splitting the file into multiple chunks and using React Query's useMutation to manage the upload of each chunk, you can effectively implement chunked file uploads for large files. This approach allows you to easily track the upload status of each chunk, and only re-upload the failed chunks when issues arise, rather than the entire file.

2024年6月29日 12:07 回复

你的答案