uploadR2R.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. window.uploadFile = async function ({ file, uuid, userid, folderid, moFolderid }) {
  2. const allowedExtensions = [
  3. "csv", "xls", "xlsx", "md", "pdf", "txt", "ppt", "pptx", "docx"
  4. ];
  5. const fileExtension = file.name.split('.').pop().toLowerCase();
  6. if (!allowedExtensions.includes(fileExtension)) {
  7. return { code: 400, msg: `不支持的文件格式: ${file.name}` }
  8. }
  9. let formData = new FormData();
  10. const timestamp = Date.now();
  11. const baseName = file.name.slice(0, -(fileExtension.length + 1));
  12. let string = [folderid, moFolderid].filter(id => id);
  13. formData.append('file', new File([file], `${baseName}${timestamp}.${fileExtension}`));
  14. formData.append('collection_ids', JSON.stringify(string));
  15. formData.append('id', uuid);
  16. formData.append('metadata', JSON.stringify({ title: file.name, collection_ids: string }));
  17. formData.append('ingestion_mode', "fast");
  18. try {
  19. await fetch("https://r2rserver.cocorobo.cn/v3/documents", {
  20. method: 'POST',
  21. body: formData,
  22. headers: {
  23. 'Accept': 'application/json',
  24. 'Content-Type': 'application/json'
  25. }
  26. });
  27. let params = {
  28. n: file.name,
  29. did: uuid,
  30. uid: userid,
  31. fid: folderid,
  32. mofid: moFolderid !== folderid ? moFolderid : ""
  33. };
  34. const res2 = await fetch('https://pbl.cocorobo.cn/api/pbl/addFile', {
  35. method: 'POST',
  36. headers: {
  37. 'Content-Type': 'application/json'
  38. },
  39. body: JSON.stringify([params])
  40. });
  41. console.log("上传成功");
  42. return { code: 200, msg: "上传成功" }
  43. } catch (err) {
  44. console.error(err);
  45. console.error("上传失败");
  46. return { code: 400, msg: "上传失败" }
  47. }
  48. }