uploadFile.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import axios from 'axios'
  2. export default {
  3. mounted() { },
  4. data() {
  5. return {
  6. uploadInfo: {},
  7. options: {
  8. server: '/file/proxyUpload',
  9. auto: true,
  10. compress: null,
  11. },
  12. upload_id: '',
  13. file: null,
  14. post_url: ''
  15. }
  16. },
  17. methods: {
  18. apiPrepareUpload(file, cb, type = 3) {
  19. let uploadInfo = null;
  20. let formData = new FormData()
  21. formData.append("filename", file.name);
  22. formData.append("target", JSON.stringify({ "type": type }));
  23. this.$axios.post('/file/prepareUpload', formData, {
  24. headers: {
  25. "Content-Type": "multipart/form-data"
  26. }
  27. }).then((data) => {
  28. this.uploadInfo = data.data.data.post_params;
  29. this.post_url = data.data.data.post_url;
  30. this.upload_id = data.data.data.upload_id;
  31. this.apiSend(file, cb,type)
  32. })
  33. return uploadInfo;
  34. },
  35. apiSend(file, cb,type = 3) {
  36. let formData = new FormData()
  37. formData.append("file", file);
  38. formData.append("name", file.name);
  39. formData.append("target", JSON.stringify({ "type": type }));
  40. for (let key in this.uploadInfo) {
  41. formData.append(key, this.uploadInfo[key])
  42. }
  43. this.$axios.post('/file/proxyUpload', formData, {
  44. headers: {
  45. "Content-Type": "multipart/form-data"
  46. }
  47. }).then(res => {
  48. this.returnData = res.data
  49. this.uploadCb(cb)
  50. })
  51. },
  52. uploadCb(cb) {
  53. let p = {
  54. upload_id: this.upload_id,
  55. return_data: JSON.stringify(this.returnData)
  56. }
  57. this.$axios.post('/file/uploadCallback', p).then(res => {
  58. cb && cb(res)
  59. })
  60. }
  61. }
  62. }