king
2020-03-18 aaee2aa047e856141dce84c5d3f1cd16d9a00dcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Upload, message, Button, Icon, Progress } from 'antd'
import Api from '@/api'
import './index.scss'
 
let service = window.GLOB.service ? (/\/$/.test(window.GLOB.service) ? window.GLOB.service : window.GLOB.service + '/') : ''
let Url = '/Upload'
if (process.env.NODE_ENV === 'production') {
  Url = document.location.origin + '/' + service + 'zh-CN/Home/Upload'
}
 
class FileUpload extends Component {
  static propTpyes = {
    value: PropTypes.array    // 按钮信息、表单列表
  }
 
  state = {
    baseUrl: Url,
    percent: 0,
    showprogress: false
  }
 
  init = async () => {
    try {
      const OSSData = await this.mockGetOSSData()
 
      this.setState({
        OSSData
      })
    } catch (error) {
      message.error(error)
    }
  }
 
  onChange = ({ fileList }) => {
    const { onChange } = this.props
 
    if (onChange) {
      onChange([...fileList])
    }
  }
 
  onRemove = file => {
    const { value, onChange } = this.props
 
    const files = value.filter(v => v.url !== file.url)
 
    if (onChange) {
      onChange(files)
    }
  }
 
  getExtraData = () => {
    return {
      RootPath: 'Content/images/upload/'
    }
  }
 
  shardupload = (file, shardSize, shardCount, i, fileList) => {
    let start = i * shardSize
    let end = Math.min(file.size, start + shardSize)
    let form = new FormData()
 
    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)
 
    if (i < shardCount) {
      i++
      Api.getFileUpload(form).then(res => {
        if (res) {
          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)
      })
    }
  }
 
  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
    } else {
      return true
    }
  }
 
  render() {
    const { value } = this.props
    const { showprogress, percent, baseUrl } = this.state
 
    const props = {
      name: 'file',
      disabled: showprogress,
      fileList: value,
      action: baseUrl,
      method: 'post',
      multiple: true,
      // headers: {'RootPath': 'Content/images/upload/'},
      onChange: this.onChange,
      onRemove: this.onRemove,
      data: this.getExtraData,
      beforeUpload: this.beforeUpload,
    }
    return (
      <Upload {...props}>
        <Button>
          <Icon type="upload" /> 点击上传
        </Button>
        {showprogress ? <Progress percent={percent} size="small" /> : null}
      </Upload>
    )
  }
}
 
export default FileUpload