| // ./services/torrentService.js |
| import axios from 'axios'; |
| |
| // IMPORTANT: Replace with your actual backend API URL |
| const API_BASE_URL = 'http://localhost:5000/api'; |
| |
| export const uploadTorrent = async (formData) => { |
| try { |
| const response = await axios.post(`${API_BASE_URL}/torrents/upload`, formData, { |
| headers: { |
| 'Content-Type': 'multipart/form-data', // Essential for file uploads |
| // Add any authentication tokens here if your backend requires them |
| // 'Authorization': `Bearer ${localStorage.getItem('authToken')}` |
| }, |
| onUploadProgress: (progressEvent) => { |
| // You can use this to show upload progress to the user |
| const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); |
| console.log(`Upload progress: ${percentCompleted}%`); |
| // You could pass this percentage back to the component via a callback |
| }, |
| }); |
| return response.data; // Your backend should return the parsed torrent info here |
| } catch (error) { |
| console.error('Error in uploadTorrent service:', error.response ? error.response.data : error.message); |
| // Throw a more specific error message based on backend response if available |
| throw new Error(error.response?.data?.message || '文件上传失败,请稍后再试。'); |
| } |
| }; |