12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import {get_filemd5sum, getBase64} from './utils.js'
-
- function FileUpLoadSdk () {
-
- const BYTES_PER_CHUNK = 1024 * 1024; // 每个文件切片大小定为1MB
- let offset = 0; //用于断点续传,默认为 0
-
- const IMAGE_TYPE_ERROR_CODE = 10
-
- const IMAGE_MULTIPART = '/multipart/upload/'
- const VIDOE_UPLOAD = '/upload/'
-
- postImage = (host, origin) => {
-
- if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(origin)) {
- return IMAGE_TYPE_ERROR_CODE
- }
-
- var index1= origin.lastIndexOf(".");
- var index2= origin.length;
- let fileType= origin.substring(index1+1,index2)
- let fileSize = origin.size
- let md5 = get_filemd5sum(origin)
- let chunkNum = Math.ceil(this.fileSize / this.BYTES_PER_CHUNK); // 分片个数
- let base64Body = getBase64(origin)
- let xhr = new XMLHttpRequest()
- xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',fileSize)
- xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1)
- xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',chunkNum)
- xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',BYTES_PER_CHUNK)
- xhr.setRequestHeader('LINK_UPLOAD_OFFSET',offset)
- xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',md5)
- xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE',fileType)
- xhr.open('post', host+IMAGE_MULTIPART+origin, true)
- xhr.send(base64Body)
-
- xhr.onreadyStateChange = function () {
- if (xhr.readystate === 4) {
- if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
- console.log('type: success, result: ', xhr.responseText)
- } else {
- console.log('type: error, errCode:', xhr.status)
- }
- }
- }
- }
-
- }
-
-
-
|