Function 将文件上载到vuejs的公用文件夹
想知道是否有办法使用vuejs编码上传公用文件夹中的文件 我有一个代码,但它是建立在移动laravel公用文件夹的文件。vuejs有这种功能吗 以下是我到目前为止关于函数的内容。希望你们能帮助我 表格代码Function 将文件上载到vuejs的公用文件夹,function,vue.js,import,Function,Vue.js,Import,想知道是否有办法使用vuejs编码上传公用文件夹中的文件 我有一个代码,但它是建立在移动laravel公用文件夹的文件。vuejs有这种功能吗 以下是我到目前为止关于函数的内容。希望你们能帮助我 表格代码 //按照此说明操作 文件 在方法属性中 handleFilesUpload(){ 让uploadedFiles=this.$refs.image2.files; 让fileExtension=uploadedFiles[0]。name.replace(/^.*\./,“”); //log(“f
//按照此说明操作
文件
在方法属性中
handleFilesUpload(){
让uploadedFiles=this.$refs.image2.files;
让fileExtension=uploadedFiles[0]。name.replace(/^.*\./,“”);
//log(“fileExtension”,fileExtension);
让allowedExtensions=/(\.jpg\.jpg\.jpeg\.jpeg\.png\.png\.pdf\.pdf\.docx)$/i;
如果(!allowedExtensions.exec(上载的文件[0].name)){
var message=“您只能上传jpg、jpeg、png、pdf和docx文件”;
此.$refs.image2.value=“”;
this.documentFiles=[];
}否则{
//log(“uploadedFiles[i]=”,uploadedFiles[0]);
//单文件上传
this.documentFiles=上载的文件;
//上传多个文件
/*
对于(var i=0;i{
console.log({res});
}).catch(错误=>{
console.error({err});
});
},
--In-Laravel控制器功能--
公共函数保存($request){
$total=@count($_FILES['FILES']['name']);
如果($total>0)
{
$allFiles=$this->uploadFiles($request);
$data['document\u file\u name']=($allFiles)?$allFiles[0]:
$result=CreditMaster::create($data);
如果($结果){
return response()->json(数组('status'=>1,
'消息'=>'数据已成功保存!');
}否则{
return response()->json(数组('status'=>0,'message'=>'Save failed!'));
}
}
}
公共函数上载文件($request)
{
$storeFileName=[];
$total=@count($_FILES['FILES']['name']);
$diretory='/save_directory_name/';
$path=public_path().$diretory;
$fileForMate=time();
对于($i=0;$i<$total;$i++){
$tmpFilePath=$\u文件['FILES']['tmp\u名称][$i];
如果($tmpFilePath!=“”){
$fileName=$fileForMate.$\文件['FILES']['name'][$i];
$newFilePath=$path.$fileName;
if(移动上传的文件($tmpFilePath,$newFilePath)){
数组推送($storeFileName,$diretory.$fileName);
}
其他的
{
返回$_文件[“文件”][“错误”];
}
}
}
返回$storeFileName;
}
<!-- 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;
});
}
}
//Follow this instruction
File
<input
type="file"
ref="image2"
v-on:change="handleFilesUpload()"
/>
In methods properties
handleFilesUpload() {
let uploadedFiles = this.$refs.image2.files;
let fileExtension = uploadedFiles[0].name.replace(/^.*\./, "");
//console.log("fileExtension", fileExtension);
let allowedExtensions = /(\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG|\.pdf|\.PDF|\.doc|\.docx)$/i;
if (!allowedExtensions.exec(uploadedFiles[0].name)) {
var message = "You can upload jpg, jpeg, png, pdf and docx file only";
this.$refs.image2.value = "";
this.documentFiles = [];
} else {
//console.log("uploadedFiles[i] = ", uploadedFiles[0]);
//Upload for single file
this.documentFiles = uploadedFiles;
//Upload for multiple file
/*
for (var i = 0; i < uploadedFiles.length; i++) {
this.documentFiles.push(uploadedFiles[i]);
}
*/
}
},
// After submit form
validateBeforeSubmit(e) {
let formData = new FormData();
for (var i = 0; i < this.documentFiles.length; i++) {
let file = this.documentFiles[i];
formData.append("files[" + i + "]", file);
}
axios.post('laravel-api-url', formData)
.then(res => {
console.log({res});
}).catch(err => {
console.error({err});
});
},
--In Laravel controller function--
public function save($request){
$total = @count($_FILES['files']['name']);
if ($total>0)
{
$allFiles = $this->uploadFiles($request);
$data['document_file_name'] = ($allFiles) ? $allFiles[0] :
$result = CreditMaster::create($data);
if ($result) {
return response()->json(array('status' => 1,
'message' => 'Data saved successfully!'));
} else {
return response()->json(array('status' => 0, 'message' => 'Save failed!'));
}
}
}
public function uploadFiles($request)
{
$storeFileName = [];
$total = @count($_FILES['files']['name']);
$diretory = '/save_directory_name/';
$path = public_path() . $diretory;
$fileForMate = time() . '_';
for ($i = 0; $i < $total; $i++) {
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
if ($tmpFilePath != "") {
$fileName = $fileForMate . $_FILES['files']['name'][$i];
$newFilePath = $path . $fileName;
if (move_uploaded_file($tmpFilePath, $newFilePath)) {
array_push($storeFileName, $diretory.$fileName);
}
else
{
return $_FILES["files"]["error"];
}
}
}
return $storeFileName;
}