king
2020-06-05 24f0ce147c8daef39ec437d5def9d089ea5b1839
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Upload, message, Button, Icon, Progress, notification } from 'antd'
import md5 from 'md5'
import Api from '@/api'
import './index.scss'
 
let Url = '/Upload'
if (process.env.NODE_ENV === 'production') {
  Url = document.location.origin + '/' + window.GLOB.service + 'zh-CN/Home/Upload'
}
 
class FileUpload extends Component {
  static propTpyes = {
    value: PropTypes.array,    // 按钮信息、表单列表
    maxFile: PropTypes.any,    // 最大文件数
    fileType: PropTypes.string // 文件显示类型
  }
 
  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 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', 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)
 
    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 {
        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
 
    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
      return true
    } else {
      return true
    }
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  render() {
    const { value, maxFile, fileType } = this.props
    const { showprogress, percent, baseUrl } = this.state
 
    let uploadable = ''
 
    if (maxFile && maxFile > 0 && value && value.length >= maxFile) {
      uploadable = 'limit-fileupload'
    }
 
    const props = {
      name: 'file',
      disabled: showprogress,
      listType: fileType,
      fileList: value,
      action: baseUrl,
      method: 'post',
      multiple: true,
      onChange: this.onChange,
      onRemove: this.onRemove,
      data: this.getExtraData,
      beforeUpload: this.beforeUpload,
      className: uploadable
    }
 
    return (
      <Upload {...props}>
        <Button>
          <Icon type="upload" /> 点击上传
        </Button>
        {showprogress ? <Progress percent={percent} size="small" /> : null}
      </Upload>
    )
  }
}
 
export default FileUpload