diff --git a/src/api/file-api.js b/src/api/file-api.js
index 20d8b2a..43a92a5 100644
--- a/src/api/file-api.js
+++ b/src/api/file-api.js
@@ -145,14 +145,6 @@ export default {
}
})
},
- // 上传文档里的图片
- uploadImage: function(data) {
- return request({
- url: 'upload-markdown-image',
- method: 'post',
- data: data
- })
- },
// 生成分享链接
generate: function(data) {
return request({
diff --git a/src/api/markdown-api.js b/src/api/markdown-api.js
index 5530c28..b413a56 100644
--- a/src/api/markdown-api.js
+++ b/src/api/markdown-api.js
@@ -50,4 +50,12 @@ export default {
data: params
})
},
+ // 上传blob格式的图片
+ uploadImage: function(data) {
+ return request({
+ url: '/upload-markdown-image',
+ method: 'post',
+ data: data
+ })
+ },
}
diff --git a/src/components/input/UploadImageInput.vue b/src/components/input/UploadImageInput.vue
index a18a19e..907d56e 100644
--- a/src/components/input/UploadImageInput.vue
+++ b/src/components/input/UploadImageInput.vue
@@ -5,8 +5,8 @@
输入图片的url 或
@@ -84,6 +86,9 @@ export default {
this.currentValue = window.location.origin + fileConfig.previewUrl(this.$store.state.user.name, row)
this.change(this.currentValue)
},
+ handleChange(file, fileList) {
+ console.log('handleChange', file, fileList)
+ },
handleBeforeUpload(file){
this.uploadState = 1
},
@@ -103,6 +108,21 @@ export default {
this.currentValue = window.location.origin + fileConfig.mardownPreviewUrl(filepath)
this.change(this.currentValue)
},
+ // url转blob
+ urlToBlob(the_url, callback) {
+ const that = this
+ let xhr = new XMLHttpRequest();
+ xhr.open("get", the_url, true);
+ xhr.responseType = "blob";
+ xhr.onload = function() {
+ if (this.status == 200) {
+ if(callback){
+ callback(this.response)
+ }
+ }
+ };
+ xhr.send();
+ },
loadSuccess() {
if(this.timer == null){
if(this.isLocked){
@@ -110,14 +130,21 @@ export default {
this.timer = setTimeout(function (){
if(!that.currentValue.startsWith(window.location.origin)){
that.uploadState = 1
- markdownApi.uploadLinkImage({
- username: that.$store.state.user.name,
- userId: that.$store.state.user.userId,
- url: that.currentValue
- }).then((res) => {
- that.format(res.data.filepath, res.data.filename)
- that.uploadState = 2
- that.$refs.uploadRef.clearFiles()
+ that.urlToBlob(that.currentValue, response => {
+ const file = new window.File(
+ [response],
+ that.getFileNameByUrl(that.currentValue),
+ { type: response.type }
+ );
+ let data = new FormData()
+ data.append("files", file)
+ data.append("username", that.$store.state.user.name)
+ data.append("userId", that.$store.state.user.userId)
+ markdownApi.uploadImage(data).then((res) => {
+ that.format(res.data[0].filepath, res.data[0].filename)
+ that.uploadState = 2
+ that.$refs.uploadRef.clearFiles()
+ })
})
}
}, 300)
@@ -126,6 +153,18 @@ export default {
clearTimeout(this.timer)
this.timer = null
}
+ },
+ // 获取url中的文件名
+ getFileNameByUrl(url){
+ const urlRgx = /[a-zA-z]+:\/\/[^\s]*/
+ if(urlRgx.test(url)){
+ if(url.indexOf("-1") > -1){
+ url = url.split('?')[0]
+ }
+ console.log(url)
+ let arr = url.split('/')
+ return arr[arr.length - 1]
+ }
}
}
}