• el-load上传


    由于el-upload控件中自定义的upload方法在上传文件中是以FormData的格式上传,后台服务器无法解析这种格式的body,所以通过http-request属性自定义了一个上传方法。

    复制代码
    <el-upload
        class="upload-demo"
         ref="upload"
        action="http://127.0.0.1:5000/json/import"
        :http-request="myUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-error="handleError"
        :on-success="handleSuccess"
        :file-list="fileList"
        :auto-upload="false">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
        <div slot="tip" class="el-upload__tip">只能上传json文件,且不超过500kb</div>
    </el-upload>
    复制代码
    复制代码
     1 myUpload(content) {
     2     console.log('myUpload...');
    this.$axios({
              method: 'post',
              url:"/lj/save/excel/2020-07-01",
              timeout: 200000,
              data: formData,
               headers:{'X-Token':getToken()}
          }).then(res => {
              content.onSuccess('文件上传成功')
    10     }).catch(error => {
    11         if (error.response) {
    12             // The request was made and the server responded with a status code
    13             // that falls out of the range of 2xx
    14             content.onError('配时文件上传失败(' + error.response.status + '),' + error.response.data);
    15         } else if (error.request) {
    16             // The request was made but no response was received
    17             // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
    18             // http.ClientRequest in node.js
    19             content.onError('配时文件上传失败,服务器端无响应');
    20         } else {
    21             // Something happened in setting up the request that triggered an Error
    22             content.onError('配时文件上传失败,请求封装失败');
    23         }
    24     });
    25 }
    复制代码

    这种方式很常见,唯一要注意的点是在上传方法调用后判断结果成功或者失败的时候,需要回调el-upload控件的onSuccess和onError方法,为的是能够复用el-upload原生的一些动作,比如如果成功了,页面上的文件列表会有一个绿勾标记上传成功的文件,如果失败则会把失败的文件从文件列表中删除,如果不回调是没有这些功能的。

  • 相关阅读:
    修改flex应用默认的装载界面 (转载)
    Map Tile 切图小工具 (转载)
    Virtual Earth Tile Image URI 参数解析(收集,学习VE插件必看)
    WorldWind学习系列十五:如何切割影像和DEM数据及其在WW中的应用配置
    ArcGIS Server建立缓存(切图)原理解析[图解] (转载)
    自制户外登山地图傻瓜书(转载)
    WorldWind学习系列十二:Measure插件学习
    MySQL备份
    aptitude包管理工具
    MySQL的Limit查询
  • 原文地址:https://www.cnblogs.com/Tian-J-Shuai/p/13777571.html
Copyright © 2020-2023  润新知