35 lines
941 B
JavaScript
35 lines
941 B
JavaScript
// File upload utility
|
|
|
|
/**
|
|
* Upload a file to the server
|
|
* @param {Object} options - Upload options
|
|
* @param {string} options.url - Upload URL
|
|
* @param {string} options.filePath - Local file path
|
|
* @param {Object} options.formData - Additional form data
|
|
* @returns {Promise} Upload result
|
|
*/
|
|
export const uploadFile = (options) => {
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: options.url,
|
|
filePath: options.filePath,
|
|
name: options.name || 'file',
|
|
formData: options.formData || {},
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
resolve(data);
|
|
} catch (e) {
|
|
resolve(res.data);
|
|
}
|
|
} else {
|
|
reject(new Error(`Upload failed with status ${res.statusCode}`));
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
}; |