king
2020-04-09 b9a0e8541f0959db5d848f7b893c8838851ce411
src/tabviews/zshare/fileupload/index.jsx
@@ -1,6 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Upload, message, Button, Icon, Progress } from 'antd'
import { Upload, message, Button, Icon, Progress, notification } from 'antd'
import md5 from 'md5'
import Api from '@/api'
import './index.scss'
@@ -62,52 +63,88 @@
  shardupload = (file, shardSize, shardCount, i, fileList) => {
    let start = i * shardSize
    let end = Math.min(file.size, start + shardSize)
    // let param = {
    //   file: file.slice(start, end),
    //   fileMd5: md5(''),
    //   shardingMd5: md5(''),
    //   baseDomain: '',
    //   rootPath: 'Content/images/upload/',
    //   fileName: file.name,
    //   fileExt: file.type.replace('image/', ''),
    //   shardingCnt: shardCount,
    //   shardingNo: i + 1
    // }
    let form = new FormData()
    let pice = file.slice(start, end)
    form.append('file', file.slice(start, end)) //slice方法用于切出文件的一部分
    form.append('RootPath', 'Content/images/upload/')
    form.append('name', file.name)
    form.append('total', shardCount)
    form.append('index', i + 1)
    form.append('file', pice) //slice方法用于切出文件的一部分
    form.append('fileMd5', md5(file))
    form.append('shardingMd5', md5(pice))
    form.append('baseDomain', '')
    form.append('rootPath', 'Content/images/upload/')
    form.append('fileName', file.name)
    form.append('fileExt', file.type.replace('image/', ''))
    form.append('shardingCnt', shardCount)
    form.append('shardingNo', i + 1)
    if (i < shardCount) {
      i++
      Api.getFileUpload(form).then(res => {
        if (res) {
    Api.getLargeFileUpload(form).then(res => {
      if (res.status) {
        if (i < shardCount) {
          i++
          this.setState({
            percent: Math.floor(100 * (i / shardCount))
          })
          this.shardupload(file, shardSize, shardCount, i, fileList)
        }
      })
    } else {
      this.setState({
        percent: 100
      }, () => {
        setTimeout(() => {
          this.setState({
            showprogress: false,
            percent: 0
          })
        }, 200)
      })
    }
      } else {
        fileList = fileList.filter(item => !!item.url)
        notification.warning({
          top: 92,
          message: res.message,
          duration: 5
        })
      }
    })
    // } else {
    //   this.setState({
    //     percent: 100
    //   }, () => {
    //     setTimeout(() => {
    //       this.setState({
    //         showprogress: false,
    //         percent: 0
    //       })
    //     }, 200)
    //   })
    // }
  }
  beforeUpload = (file, fileList) => {
    let shardSize = 2 * 1024 * 1024
    // let shardSize = 3 * 1024
    if (file.size > shardSize) {
      this.setState({
        showprogress: true,
        percent: 0
      })
      let shardCount = Math.ceil(file.size / shardSize)
      this.shardupload(file, shardSize, shardCount, 0, fileList)
      return false
      // this.setState({
      //   showprogress: true,
      //   percent: 0
      // })
      // let shardCount = Math.ceil(file.size / shardSize)
      // this.shardupload(file, shardSize, shardCount, 0, fileList)
      // return false
      return true
    } else {
      return true
    }
  }
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
@@ -129,7 +166,6 @@
      action: baseUrl,
      method: 'post',
      multiple: true,
      // headers: {'RootPath': 'Content/images/upload/'},
      onChange: this.onChange,
      onRemove: this.onRemove,
      data: this.getExtraData,