【发布时间】:2020-03-26 07:48:39
【问题描述】:
想知道是否有办法使用 vuejs 编码在公共文件夹中上传文件 我确实有一个代码,但它是为了在 laravel 公共文件夹中移动文件而构建的。 vuejs有这种功能吗?
这是我目前的功能。希望大家帮帮我
表单代码
<!-- Form Upload -->
<div class="row">
<div class="col-sm-6 offset-3">
<div class="form-group">
<label for="exampleFormControlFile1">Upload</label>
<input
type="file"
ref="file"
class="form-control-file"
@change="onFileChange"
>
<small class="text-muted">
for slideshow images
<br />
size: 1280 x 630 pixel for good quality
</small>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-primary"
data-dismiss="modal"
@click="uploadFile"
:disabled="disableBtn"
>Upload</button>
</div>
方法代码
onFileChange(e) {
this.file = this.$refs.file.files[0];
},
uploadFile() {
if (this.file == "") {
this.alertClass = "alert alert-danger";
this.alertMessage = "Please select a file";
this.showAlert = true;
} else {
this.disableBtn = true;
this.$parent.showLoading();
let requestUrl =
this.baseUrl + "/media";
let formData = new FormData();
formData.append("file", this.file,);
formData.append("mediatype", this.Mediatype);
let headers = {
headers: {
"Content-Type": "multipart/form-data"
}
};
this.$http
.post(requestUrl, formData, headers)
.then(response => {
this.alertClass = "alert alert-success";
this.alertMessage = response.data.message;
this.$refs.file.value = ''
this.showAlert = true;
this.$parent.hideLoading();
this.disableBtn = false;
this.$parent.getGallery();
})
.catch(() => {
this.disableBtn = false;
this.$parent.hideLoading();
this.alertClass = "alert alert-danger";
this.alertMessage =
"There is a problem in the request.";
this.showAlert = true;
});
}
}
【问题讨论】: