金格iweboffice axios上传单文件和多文件js代码
生活随笔
收集整理的這篇文章主要介紹了
金格iweboffice axios上传单文件和多文件js代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
base64toBlob函數
base64toBlob (base64Data, contentType) {contentType = contentType || ''var sliceSize = 1024var byteCharacters = atob(base64Data)var bytesLength = byteCharacters.lengthvar slicesCount = Math.ceil(bytesLength / sliceSize)var byteArrays = new Array(slicesCount)for (var sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {var begin = sliceIndex * sliceSizevar end = Math.min(begin + sliceSize, bytesLength)var bytes = new Array(end - begin)for (var offset = begin, i = 0; offset < end; ++i, ++offset) {bytes[i] = byteCharacters[offset].charCodeAt(0)}byteArrays[sliceIndex] = new Uint8Array(bytes)}return new Blob(byteArrays, { type: contentType }) }單文件上傳
let base64file = this.webOfficeObj.obj.File2Base64Text(data.path)let myfff = this.base64toBlob(base64file)let formData = new FormData()formData.append('file', myfff, this.webOfficeObj.FileName)axios({method: 'post',url: url || this.webOfficeObj.WebUrl,data: formData,headers: {Accept: 'application/json, text/plain, */*',Authorization: 'bearer ' + this.$http.getAccessToken(),'Content-Type': 'multipart/form-data'}}).then(({data}) => {if (data && data.code === 0) {this.$emit('upload-success', data)} else {this.$message({message: data.msg,type: 'error',duration: 1500})}}).catch(() => {this.$message.error('文件上傳失敗')})多文件上傳
let formData = new FormData()data.fileList.forEach(item => {let base64file = this.webOfficeObj.obj.File2Base64Text(item.path)let myfff = this.base64toBlob(base64file)formData.append('file[]', myfff, item.fileName)})axios({method: 'post',url: url || this.webOfficeObj.WebUrl,data: formData,headers: {Accept: 'application/json, text/plain, */*',Authorization: 'bearer ' + this.$http.getAccessToken(),'Content-Type': 'multipart/form-data'}}).then(({data}) => {if (data && data.code === 0) {this.$emit('upload-success', data)} else {this.$message({message: data.msg,type: 'error',duration: 1500})}}).catch(() => {this.$message.error('文件上傳失敗')})總結
以上是生活随笔為你收集整理的金格iweboffice axios上传单文件和多文件js代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux使用strlen编译,strl
- 下一篇: iptv错误代码2003什么意思_IPT