Selaa lähdekoodia

发送图片优化

wangwei 4 vuotta sitten
vanhempi
commit
71a5039f1c
4 muutettua tiedostoa jossa 33 lisäystä ja 36 poistoa
  1. 1 3
      src/api/http.js
  2. 1 1
      src/api/require.js
  3. 31 30
      src/components/ChatFrame.vue
  4. 0 2
      src/store/modules/app/actions.js

+ 1 - 3
src/api/http.js

@@ -140,15 +140,13 @@ export default {
     });
   },
   upload: function(path = '', file = {}) {
-    console.log('=============xxxxx==')
-    console.log(`data`, file)
     let fd = new FormData();
     fd.append("filename", file);
     return new Promise(function(resolve, reject) {
       // const token = cookie.get('token')
       axios.post(path, fd, {
         headers:{
-          'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryUb95bwwxDSCn3mSq',
+          "Content-Type": "multipart/form-data"
         }
       })
         .then(function(response) {

+ 1 - 1
src/api/require.js

@@ -6,6 +6,6 @@ import urls from './urls'
 export default  {
   test: (params) => http.get(urls.login_url, params),
   login: (params) => http.post(urls.login_url, params),
-  upload_file: (params) => http.post(urls.upload_url, params),
   register: (params) => http.post(urls.register_url, params),
+  upload_file: (params) => http.upload(urls.upload_url, params),
 }

+ 31 - 30
src/components/ChatFrame.vue

@@ -341,7 +341,8 @@ export default {
   },
   data() {
     return {
-      baseUrl: "http://192.168.100.8:3000/",
+      // baseUrl: "http://192.168.100.8:3000/",
+      baseUrl: "http://127.0.0.1:3000/",
       emojiIcon: emojiData.icon,
       inputRange: "", // 光标
       emojiPath: new Map(), // emoji表情地址map对象,
@@ -403,22 +404,11 @@ export default {
       //  调用函数分割文件 我这里是分割成不超过20M的文件快
       this.fileDataList = this.createFileChunk(file, 1024 * 1024 * 20);
       console.log(`this.fileDataList`, this.fileDataList);
-      // return new Promise((reslove, reject)=>{
-      //   this.fileUpLoad(reslove, reject);
-      // })
-      let fd = new FormData();
-      fd.append("filename", file);
-      // upload_file(fd).then(
-      request
-        .upload_file(file)
+      
+      request.upload_file(file)
         .then(res => {
-          console.log(`res5555`, res);
-        })
-        .catch(err => {
-          console.log(`err`, err);
-        });
-      this.$http.post(this.baseUrl + "api/upload/", fd).then(
-        res => {
+          console.log(`res`, res)
+          console.log('============上传成功=======')
           if (
             file.type === "image/jpeg" ||
             file.type === "image/png" ||
@@ -426,23 +416,34 @@ export default {
           ) {
             this.messageType = "image";
             this.sendMsg(null, res.data.data);
-            console.log(`res.data.data`, res.data.data);
-            console.log("========is image =====");
           } else {
             this.messageType = "file";
-            console.log("=========not image=========");
           }
-          // this.importDataBtnText='导入成功';
-          console.log(`res1111`, res);
-          console.log("======success====");
-        },
-        res => {
-          this.$message.error("文件发送失败");
-          console.log("===========shibai====");
-          //  this.importDataBtnText='导入失败';
-          console.log(res);
-        }
-      );
+        })
+        .catch(err => {
+          console.log(`upload----err`, err);
+          console.log('===========上传失败2333-----')
+        });
+      // this.$http.post(this.baseUrl + "api/upload/", fd).then(
+      //   res => {
+      //     if (
+      //       file.type === "image/jpeg" ||
+      //       file.type === "image/png" ||
+      //       file.type === "image/gif"
+      //     ) {
+      //       this.messageType = "image";
+      //       this.sendMsg(null, res.data.data);
+      //     } else {
+      //       this.messageType = "file";
+      //     }
+      //   },
+      //   res => {
+      //     this.$message.error("文件发送失败");
+      //     console.log("===========shibai====");
+      //     //  this.importDataBtnText='导入失败';
+      //     console.log(res);
+      //   }
+      // );
       return false;
     },
     submitUpload() {

+ 0 - 2
src/store/modules/app/actions.js

@@ -1,6 +1,4 @@
-// import axios from '@/api/axios';
 import request from '@/api/require'
-import Vue from 'vue';
 import { processReturn } from '@/utils/common';
 import { SET_USER, SET_TOKEN, SET_LOADING } from './mutation-types';