ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 1 | // ./services/torrentService.js |
| 2 | import axios from 'axios'; |
| 3 | |
| 4 | // IMPORTANT: Replace with your actual backend API URL |
| 5 | const API_BASE_URL = 'http://localhost:5000/api'; |
| 6 | |
| 7 | export const uploadTorrent = async (formData) => { |
| 8 | try { |
| 9 | const response = await axios.post(`${API_BASE_URL}/torrents/upload`, formData, { |
| 10 | headers: { |
| 11 | 'Content-Type': 'multipart/form-data', // Essential for file uploads |
| 12 | // Add any authentication tokens here if your backend requires them |
| 13 | // 'Authorization': `Bearer ${localStorage.getItem('authToken')}` |
| 14 | }, |
| 15 | onUploadProgress: (progressEvent) => { |
| 16 | // You can use this to show upload progress to the user |
| 17 | const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); |
| 18 | console.log(`Upload progress: ${percentCompleted}%`); |
| 19 | // You could pass this percentage back to the component via a callback |
| 20 | }, |
| 21 | }); |
| 22 | return response.data; // Your backend should return the parsed torrent info here |
| 23 | } catch (error) { |
| 24 | console.error('Error in uploadTorrent service:', error.response ? error.response.data : error.message); |
| 25 | // Throw a more specific error message based on backend response if available |
| 26 | throw new Error(error.response?.data?.message || '文件上传失败,请稍后再试。'); |
| 27 | } |
| 28 | }; |