1. 程式人生 > 其它 >java學習之Java工具的分享

java學習之Java工具的分享

   uploadImg (file) { // 上傳圖片 拿到上傳檔案,調介面,介面返回圖片url,name等資訊。
    // (這裡注意content-type在上傳檔案或者圖片的時為mutipart/form-data型別,headers可攜帶token等資訊)
    // post 方法 content-type 為apllication/x-www-form-urcoded let param = new FormData() param.append('accountToken', this.$localStorageUtil.getHrAccountInfo().accountToken) param.append('file', this.uploadData.file, this.uploadData.file.name) this.$axios( { method: 'post', headers: { hrToken: this.$localStorageUtil.getHrAccountInfo().accountToken }, url: motivationApi.uploadNoticeImg, data: param } ).then((res) => { if (res.data && (res.data.return_code === '00')) { this.uploadData.url = res.data.data.fileUrl this.formValidate.coverImage = res.data.data.fileUrl this.uploadData.fileName = res.data.data.fileName } else { this.$Message.warning(res.data.return_msg) } }).catch(error => { console.log(error) }) }, handleBeforeUpload (file) { // let imgFormatArr = ['jpg', 'jpeg', 'png', 'gif'] let type = file.type.split('/') let size = file.size / 1024 / 1024 // M // 上傳圖片大小 if (type && type[0] === 'image' && imgFormatArr.includes(type[1]) && size <= 2) { // 上傳圖片格式和大小限制 this.uploadData.url = window.URL.createObjectURL(file) // https://test-api.fafuli.com/ 這裡通過瀏覽器建立了一個以blob://開頭的的本地圖片連結 document.getElementById('uploadCoverImg').style.backgroundImage = "url('" + this.uploadData.url + "')"
        //上一行程式碼意思是 拿到本地圖片連結,給id為uploadcoverimg的div設定背景圖片樣式 this.formValidate.coverImage = this.uploadData.url // 校驗內容 this.uploadData.file = file this.uploadImg(file) return false // 最後return false 跳出 } else { this.$Message.warning('上傳圖片格式有誤!') } }