1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
| <template>
<div>
<form @submit.prevent="sendFile" enctype="multipart/form-data">
<div v-if="message"
:class="`message ${error ? 'is-danger': 'is-success'}`"
>
<div class="message-body">
{{message}}
</div>
</div>
<div class="dropzone" v-if="uploadvisible">
<input
:accept="this.accept"
multiple
type="file"
class="input-file"
ref="files"
@change="selectFile"
/>
<p v-if="!uploading" class="call-to-action">
Cliquez ici pour ajouter vos fichiers ou déposez les ici
</p>
</div>
</form>
<div v-if="fileExist">
<h2 class="is-size-2">Fichier en cours de transfert</h2>
<div class="columns" v-for="file in ToUpload" :key="file.name">
<div class="column">
{{file.name}}
</div>
<div class="column">
<progress
class="progress is-primary"
:value="progress[file.id]"
max="100"
>
{{progress[file.id]}} %
</progress>
</div>
</div>
</div>
<div v-if="visible" class="row">
<div class="col-md-12">
<draggable :list="this.jsonfile" ghost-class="ghost" class="list-group" @end="onEnd">
<transition-group type="transition" class="list-group-item" name="flip-list">
<div class="row pl-3 pt-3 pb-3 pr-3 multiplepicture sortable" v-for="json in this.jsonfile" :key="json.id" :id="json.id">
<div class="col-md-2 vcenter">
<a :href="json.storage" target="_blank">Consultez le fichier</a>
<input type="hidden" :name="`files[${json.id}][id]`" :value="json.id" />
</div>
<div class="col-md-6 vcenter">
<div class="row form-group">
<div class="col-md-12">
<label for="title">Titre du fichier</label>
</div>
<div class="col-md-12">
<input class="form-control form-control-label" autofocus id="inputTitle" placeholder="Indiquez le titre du fichier" :name="`files[${json.id}][title]`" type="text" :value="json.name" />
</div>
</div>
</div>
<div class="col-md-3 vcenter">
<div v-if="json.checked == 1 && json.positive == 0" class="btn btn-success">
Fichier vérifié
</div>
<div v-if="json.checked == 0" class="btn btn-warning">
Le fichier sera<br /> analysé<br />automatiquement<br />par un antivirus
</div>
<div v-if="json.checked == 1 && json.positive == 1" class="btn btn-danger">
Fichier vérolé
</div>
</div>
<div class="col-md-1 vcenter">
<a class="delete is-medium is-danger" :data-key="json.id" v-on:click="removeElement(json.id)">X</a>
</div>
</div>
</transition-group>
</draggable>
</div>
</div>
</div>
</template>
<script>
import _ from "lodash"
import axios from 'axios';
axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};
export default {
name: "FilemultipledropzoneUpload",
props : {
accept: {type: String, required: false, default: '*'},
url: {type: String, required: true},
json: String,
deleteurl: {type: String, required: true},
hiddenid : {type: String, required: true},
maxfile : {type: Number, required: false, default: 10}
},
data () {
return {
files: [],
message: "",
error: false,
uploading: false,
uploadFiles: [],
uploadedFiles: [],
progress: {},
fileExist: false,
allowedType: [],
ToUpload: [],
visible: false,
fileUpload: false,
jsonfile: [],
uploadvisible: true,
numfile: 0,
position: 0,
oldIndex: '',
newIndex: '',
}
},
mounted() {
this.AcceptedTransform();
this.JsonFile();
},
methods: {
onEnd(evt) {
this.oldIndex = evt.oldIndex;
this.newIndex = evt.newIndex;
let position = 1;
this.jsonfile.forEach(element => {
element.position = position;
position = position + 1
});
},
JsonFile(){
this.visible = true;
this.numfile = JSON.parse(this.json).length;
if(this.numfile >= this.maxfile) {
this.uploadvisible = false;
}
this.jsonfile = JSON.parse(this.json);
},
AcceptedTransform() {
this.allowedTypes = this.accept.split(',');
},
get_extension(filename) {
return filename.slice((filename.lastIndexOf('.') - 1 >>> 0) + 2);
},
selectFile() {
const files = this.$refs.files.files;
if (this.maxfile < (files.length + this.numfile)){
alert('Impossible de mettre plus de '+this.maxfile+' fichiers dans cet article');
}else {
this.fileExist = true;
this.ToUpload = [
...this.ToUpload,
...this.files,
..._.map(files, file => ({
id: Math.random().toString(36).substr(2, 9),
content: file,
name: file.name,
size: file.size,
type: file.type,
invalidMessage: this.validate(file)
}))
];
_.forEach(this.ToUpload, file => {
this.$set(this.progress, file.id, 0);
})
this.sendFile();
}
},
async sendFile() {
_.forEach(this.ToUpload, async file => {
if(file.invalidMessage) {
const formData = new FormData();
formData.append('file', file.content);
const res = await axios.post(this.url, formData, {
onUploadProgress: e => this.progress[file.id] = Math.round(e.loaded * 100 / e.total)
});
if (res.status === 200) {
res.data.position = this.jsonfile.length + 1;
this.jsonfile = this.jsonfile.concat(await res.data);
//suppression de l'ensemble des valeurs dans le toUpload ????
this.ToUpload.splice(this.ToUpload.find(f => f.id === file.id), 1);
// Récupération des informations de l'upload pour suite traitement
this.uploadedFiles.push(res.data.file);
if (this.ToUpload.length === 0){
this.fileExist = false;
}
this.fileUpload = true;
}
}else{
this.ToUpload.splice(this.ToUpload.find(f => f.id === file.id), 1);
if (this.ToUpload.length === 0){
this.fileExist = false;
}
}
})
this.numfile = this.jsonfile.length + 1;
if(this.numfile >= this.maxfile) {
this.uploadvisible = false;
}
},
validate (file) {
const extension = '.'+this.get_extension(file.name.toLowerCase());
if (this.allowedTypes.includes(extension) || this.accept === "*") {
return true;
} else {
return false;
}
},
async removeElement(idp){
const formData = new FormData();
formData.append('id', idp);
let urldelete = this.deleteurl.replace('{id}', idp);
const res = await axios.post(urldelete, formData, {
});
if (res.status === 200) {
this.jsonfile = this.jsonfile.filter(o => o.id !== idp);
this.numfile = this.jsonfile.length;
if (this.numfile < this.maxfile) {
this.uploadvisible = true;
}
}
let position = 1;
this.jsonfile.forEach(element => {
element.position = position;
position = position + 1
});
}
}
}
</script>
<style>
.dropzone{
min-height: 200px;
padding: 10px;
position: relative;
cursor: pointer;
outline: 2px dashed grey;
/*outline-radius: 25px;*/
outline-offset: -10px;
background-color: lightcyan;
color: grey;
/*border-radius: 25px;*/
}
.dropzone:hover{
background-color: lightblue;
}
.dropzone .call-to-action{
font-size: 1.2rem;
text-align: center;
vertical-align: middle;
padding-top: 70px;
}
.dropzone .progress-bar{
text-align: center;
padding: 70px 10px;
}
.input-file{
opacity: 0;
width: 100%;
height: 200px;
position: absolute;
cursor: pointer;
}
table{
width: 100%;
}
.delete{
cursor: pointer;
}
</style> |
Partager