• eladmin 如何实现文件的上传


     前端页面

    1. <script>
    2. import { uploadFile } from "@/utils/api";
    3. export default {
    4. data() {
    5. return {
    6. fileList: [],
    7. formData: "",
    8. }
    9. },
    10. created() { },
    11. mounted: function () {
    12. },
    13. methods: {
    14. submitUpload() {
    15. this.$refs.upload.submit();
    16. },
    17. handleRemove(file, fileList) {
    18. console.log(file, fileList);
    19. },
    20. handlePreview(file) {
    21. console.log(file);
    22. },
    23. delFile() {
    24. this.fileList = [];
    25. },
    26. handleChange(file, fileList) {
    27. this.fileList = fileList;
    28. },
    29. //自定义上传文件
    30. uploadFile(file) {
    31. //this.formData.append("file", file.file);
    32. },
    33. //删除文件
    34. handleRemove(file, fileList) {
    35. console.log(file, fileList);
    36. },
    37. // 点击文件
    38. handlePreview(file) {
    39. console.log(file);
    40. },
    41. //保存按钮
    42. async onSubmit() {
    43. let formData = new FormData();
    44. formData.append("file", this.fileList[0].raw);//拿到存在fileList的文件存放到formData中
    45. //formData.append("title", this.title);
    46. await hxn3bUploadFile(formData).then(res => {
    47. this.$message({
    48. message: res,
    49. type: 'success'
    50. });
    51. this.$emit("closeUpload")
    52. }).catch(
    53. res => {
    54. this.$message({
    55. message: res,
    56. type: 'error'
    57. });
    58. }
    59. )
    60. }
    61. }
    62. }
    63. script>
    64. <style scoped>
    65. .uploadFile {
    66. text-align: center;
    67. }
    68. style>

     封装api

    1. import requestMultipart from './httpsMultipart'
    2. export function uploadFile(data) {
    3. return requestMultipart({
    4. url: '/api/upload',
    5. method: 'post',
    6. data
    7. })
    8. }

     封装https

    1. const requestMultipart = axios.create({
    2. baseURL: process.env.VUE_APP_BASE_API,
    3. timeout: Config.timeout
    4. })
    5. requestMultipart.interceptors.request.use(
    6. config => {
    7. if (getToken()) {
    8. config.headers['Authorization'] = getToken()
    9. }
    10. config.headers['Content-Type'] = "multipart/form-data;charset=utf-8";
    11. return config
    12. },
    13. error => {
    14. Promise.reject(error)
    15. }
    16. )
    17. requestMultipart.interceptors.response.use(
    18. response => {
    19. return response.data
    20. },
    21. error => {
    22. if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
    23. const reader = new FileReader()
    24. reader.readAsText(error.response.data, 'utf-8')
    25. reader.onload = function (e) {
    26. const errorMsg = JSON.parse(reader.result).message
    27. Notification.error({
    28. title: errorMsg,
    29. duration: 5000
    30. })
    31. }
    32. } else {
    33. let code = 0
    34. try {
    35. code = error.response.data.status
    36. } catch (e) {
    37. if (error.toString().indexOf('Error: timeout') !== -1) {
    38. Notification.error({
    39. title: '网络请求超时',
    40. duration: 5000
    41. })
    42. return Promise.reject(error)
    43. }
    44. }
    45. console.log(code)
    46. if (code) {
    47. if (code === 401) {
    48. store.dispatch('LogOut').then(() => {
    49. Cookies.set('point', 401)
    50. location.reload()
    51. })
    52. } else if (code === 403) {
    53. router.push({ path: '/401' })
    54. } else {
    55. const errorMsg = error.response.data.message
    56. if (errorMsg !== undefined) {
    57. Notification.error({
    58. title: errorMsg,
    59. duration: 5000
    60. })
    61. }
    62. }
    63. } else {
    64. Notification.error({
    65. title: '接口请求失败',
    66. duration: 5000
    67. })
    68. }
    69. }
    70. return Promise.reject(error)
    71. }
    72. )
    73. export default requestMultipart

    后端方法 

    1. @PostMapping(具体路径)
    2. public String saveVue(String title,@RequestParam("file") MultipartFile file) throws IOException {
    3. //拿到具体文件 file
    4. return "SUCCESS";
    5. }

  • 相关阅读:
    Pruning Pre-trained Language Models Without Fine-Tuning
    Verilog task使用说明
    计算机二级C语言经典资料汇总
    JAVA计算机毕业设计作业管理系统Mybatis+系统+数据库+调试部署
    硬件内存模型
    Self-supervised Video Transformer 阅读
    Veritas Backup Exec v22.2.1193.1605 数据备份恢复软件
    低/无代码开发系统集成能力有多强?一文告诉你
    Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
    AIGC绘本——海马搬家来喽
  • 原文地址:https://blog.csdn.net/qq_36978986/article/details/132873708