diff --git a/src/components/vue-file-agent-mixin.js b/src/components/vue-file-agent-mixin.js index b0a08e5..6c04bba 100644 --- a/src/components/vue-file-agent-mixin.js +++ b/src/components/vue-file-agent-mixin.js @@ -101,6 +101,12 @@ export default { } return uploader.deleteUpload(url, headers, fileData, uploadData); }, + renameUpload(url, headers, fileData, uploadData){ + if(this.filesData.length < 1){ + this.overallProgress = 0; + } + return uploader.renameUpload(url, headers, fileData, uploadData); + }, autoUpload(filesData){ if(!this.uploadUrl){ return; @@ -113,6 +119,12 @@ export default { } return this.deleteUpload(this.uploadUrl, this.uploadHeaders, fileData); }, + autoRenameUpload(fileData){ + if(!this.uploadUrl){ + return Promise.resolve(false); + } + return this.renameUpload(this.uploadUrl, this.uploadHeaders, fileData); + }, equalFiles(file1, file2){ return true && file1.name === file2.name && @@ -252,6 +264,11 @@ export default { this.filesData.splice(i, 1, fileData); }); }, + filenameChanged(fileData){ + this.$emit('rename', FileData.toRawArray([fileData])[0]); + this.autoRenameUpload(fileData).then((res)=> { }, (err)=> { + }); + }, checkValue(){ var filesDataRaw = this.value || []; filesDataRaw = Array.isArray(filesDataRaw) ? filesDataRaw : [filesDataRaw]; diff --git a/src/components/vue-file-agent.vue b/src/components/vue-file-agent.vue index bc71bf1..e651b3a 100644 --- a/src/components/vue-file-agent.vue +++ b/src/components/vue-file-agent.vue @@ -12,8 +12,8 @@ diff --git a/src/components/vue-file-preview.vue b/src/components/vue-file-preview.vue index 1ebe61e..4ff1633 100644 --- a/src/components/vue-file-preview.vue +++ b/src/components/vue-file-preview.vue @@ -145,8 +145,6 @@ }, editInputBlured(){ - var value = this.$refs.input.value; - this.fileData.customName = value; var timeout = 100; setTimeout(()=> { this.$nextTick(()=> { @@ -160,6 +158,12 @@ filenameChanged(completed){ if(completed){ + var newValue = this.$refs.input.value; + var oldValue = this.fileData.customName ? this.fileData.customName : this.fileData.name(true); + if (newValue !== oldValue) { + this.fileData.customName = newValue; + this.$emit('rename', this.fileData); + } this.$refs.input.blur(); } if(completed === false){ diff --git a/src/lib/ajax-request.js b/src/lib/ajax-request.js index 8507452..484dc55 100644 --- a/src/lib/ajax-request.js +++ b/src/lib/ajax-request.js @@ -132,6 +132,10 @@ class AjaxRequest { return this.request('DELETE', url, formData, configureFn); } + put(url, formData, configureFn = null){ + return this.request('PUT', url, formData, configureFn); + } + } export default new AjaxRequest(); \ No newline at end of file diff --git a/src/lib/upload-helper.js b/src/lib/upload-helper.js index 71cceb8..becb9ea 100644 --- a/src/lib/upload-helper.js +++ b/src/lib/upload-helper.js @@ -36,6 +36,17 @@ class UploadHelper { }); } + doRenameUpload(url, headers, data, configureFn){ + if (typeof data != 'string') { + data = JSON.stringify(data); + } + return ajax.put(url, data, (xhr)=> { + xhr.setRequestHeader('Content-Type', 'application/json'); + this.addHeaders(xhr, headers); + configureFn(xhr); + }); + } + doUploadAxios(axios, formData, progressCallback){ return axios.post('/upload', formData, { onUploadProgress: progressCallback, @@ -128,6 +139,27 @@ class UploadHelper { }); } + renameUpload(url, headers, fileData, uploadData){ + return new Promise((resolve, reject)=> { + if (fileData.xhr) { + fileData.xhr.abort(); + } + if(uploadData === undefined){ + uploadData = fileData.upload; + uploadData.customName = fileData.customName; + } + if (uploadData) { + this.doRenameUpload(url, headers, uploadData, (xhr)=> { + }).then((result)=> { + resolve(result); + }, (err)=> { + this.prepareUploadError(fileData, err); + reject(err); + }); + } + }); + } + } export default new UploadHelper(); \ No newline at end of file