文件上传服务器、文件展示等异步问题

文章描述了一个在文件上传过程中,当文件上传完毕但服务器响应未返回时,如何通过前端阻塞进度条并等待服务器结果来获取文件路径的问题及其解决方案,包括使用Vue组件如ElUpload和自定义方法处理上传进度和成功回调。
摘要由CSDN通过智能技术生成

问题:

文件上传模块:当文件已经上传完成,文件进度已经走完了,但是服务器响应还没有返回结果,出现了,获取不到上传后的文件路径,需要等待服务器返回结果后,才能获取文件路径并点击跳转到指定的下载地址。

【时机:文件进度已经100%了但是服务器返回结果还没有返回、获取不到跳转地址】

解决方案:

1.区分有跳转链接和没有链接的text 文本的颜色

2.前端阻塞:当没有服务器没有返回结果的时候,前端手动阻塞 进度条的进度 为 95%,当文件服务器返回结果后,然后手动将进度条置为 100%;

【下面为部分 核心代码】

 <div class="files" ref="filesRef">
        <div class="files_title">附件列表</div>
        <div class="file" v-for="(item, index) in multipartFiles" :key="item.fileName">
          <div style="display: flex;flex-direction: column;width: 100%">
            <div style="display: flex;justify-content: space-between;align-items: center">
              <div style="display: flex;justify-content: flex-start;align-items: center;">
                <el-icon v-if="item.percentage===100 && item.response" color="rgba(0,0,0,.45)" size="14">
                  <Link/>
                </el-icon>
                <el-icon v-else class="is-loading">
                  <Loading/>
                </el-icon>
                <a v-if="item.response" :href="item.url" class="name contents" target="_blank">{{
                    item.name
                  }}</a>
                <span v-else class="name contents" style="color: grey;pointer-events: none">{{
                    item.name
                  }}</span>
              </div>
              <el-icon color="rgba(0,0,0,.45)" size="14" @click="handleDelMul(index)">
                <Delete/>
              </el-icon>
            </div>
            <div style="margin-top: 4px">
              <!--              5% 等待服务端返回。    核心操作 -->
              <el-progress v-if="!item.response"
                           :percentage="item.response&&uploadCompleted?100:Math.max(0,item.percentage - 5)"/>
            </div>
          </div>
        </div>
      </div>
      <ElUpload
          style="margin-left: 2px"
          class="upload-demo"
          :action="action"
          :headers="{
	          'X-Access-Token': token,
          }"
          :on-progress="onProgress"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-error="handleUploadError"
          :on-success="handleUploadSuccess"
          multiple
      >
        <template #trigger>
          <ElButton size="small">上传附件</ElButton>
        </template>
      </ElUpload>

进度条方法:

   function onProgress(progressEvent, file) {
      if (multipartFiles.value.filter(item => item.uid === file.uid).length === 0) {
        multipartFiles.value.push(file)
      }
      // 计算上传进度百分比
      file.percentage = parseInt(Math.round((progressEvent.loaded * 100) / progressEvent.total));
    }

其他方法

    function handleUploadError(error) {
      console.log('上传失败', error);
    }
    
    const uploadCompleted = ref(false)
    
    async function handleUploadSuccess(e) {
      console.log('上传成功', e.result, multipartFiles.value);
      if (e.code === 200) {
        uploadCompleted.value = true;
        //这里处理上传成功后的逻辑
        const res = mergeArrays(multipartFiles.value, [e.result])
        console.log(res, 'res')
        multipartFiles.value = res;
      }
    }
    
    // 合并数组并处理重复项 --- 特殊处理
    function mergeArrays(array1, array2) {
      // 合并数组并处理重复项
      return array1.map((item1) => {
        const item2 = array2.find(
            (item2) => item2.name === item1.name && item2.name === item1.name
        );
        if (item2) {
          return {...item1, ...item2};
        } else {
          return item1;
        }
      });
    }

效果:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值