aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/form_inputs/FileUpload.vue
blob: d776a81868c6431e382c1bcb404865daf88ba1a7 (plain)
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
<template>
    <section>
        <button v-show="!uploading"
               class="button select"
               :class="{studiprequired: required}"
               @click.prevent="openFileSelect">
            <studip-icon shape="upload"></studip-icon>
            <span class="textlabel">
                {{ title }}
            </span>
            <span v-if="required"
                  class="asterisk"
                  :title="$gettext('Dies ist ein Pflichtfeld')"
                  aria-hidden="true">*</span>
        </button>
        <div class="file-count">
            <template v-if="selectedFiles?.length === 0">
                {{ $gettext('Keine Dateien gewählt') }}
            </template>
            <template v-else-if="selectedFiles?.length === 1">
                {{ $gettext('Eine Datei gewählt') }}
            </template>
            <template v-else>
                {{ $gettext('%{number} Dateien gewählt', { number: selectedFiles.length }) }}
            </template>
        </div>
        <input type="file"
               :name="name"
               :id="id"
               :multiple="multiple"
               :accept="accept"
               ref="files"
               class="button"
               @change="selectFiles">
        <button v-if="selectedFiles.length > 0"
                type="button"
                class="button upload"
                @click.prevent="upload">
            <studip-icon shape="upload"></studip-icon>
            {{ $gettext('Jetzt hochladen') }}
        </button>
        <div v-if="!uploading && uploadedFiles.length > 0">
            <span>
                {{ $gettext('Bereits hochgeladen:') }}
            </span>
            <ul>
                <li v-for="(file, index) in uploadedFiles"
                    :key="index">
                    {{ file.name + ' (' + getTextualFileSize(file.size) + ')' }}
                </li>
            </ul>
        </div>
        <input type="hidden"
               :name="name"
               :value="targetFolder">
        <studip-progress-indicator v-if="uploading"
                                   :size="24">
            {{ $gettext('Wird hochgeladen...') }}
        </studip-progress-indicator>
    </section>
</template>

<script>
import axios from 'axios';
import StudipProgressIndicator from "../StudipProgressIndicator.vue";

export default {
    name: 'FileUpload',
    components: {StudipProgressIndicator},
    props: {
        name: {
            type: String,
            required: true
        },
        title: {
            type: String,
            required: true
        },
        folder: {
            type: String,
            required: true
        },
        uploadUrl: {
            type: String,
            required: true
        },
        id: {
            type: String
        },
        required: {
            type: Boolean,
            default: false
        },
        multiple: {
            type: Boolean,
            default: false
        },
        accept: {
            type: String,
            default: '*/*'
        }
    },
    data() {
        return {
            selectedFiles: [],
            uploading: false,
            uploadedFiles: [],
            targetFolder: ''
        }
    },
    methods: {
        upload() {
            if (this.$refs.files.files.length > 0) {
                this.uploading = true;

                const files = this.$refs.files.files;

                const formData = new FormData();

                let name = this.name;
                if (this.multiple) {
                    name += '[]';
                }

                for (let i = 0; i < files.length; i++) {
                    formData.append(name, files[i]);
                    this.uploadedFiles.push(files[i]);
                }

                axios.post(
                    this.uploadUrl,
                    formData
                ).then(() => {
                    this.uploading = false;
                    this.$refs.files.value = '';
                    this.targetFolder = this.folder;
                }).catch(error => {
                    this.uploadedFiles = [];
                    this.uploading = false;
                    STUDIP.Report.error(this.$gettext('Fehler beim Hochladen'), error);
                });
            }
        },
        getTextualFileSize(bytes) {
            if (bytes < 1024) {
                return this.$gettext('%{size} B', {size: bytes});
            }

            if (bytes < 1024 * 1024) {
                return this.$gettext('%{size} KB', {
                    size: (bytes / 1024).toFixed(2)
                });
            }

            return this.$gettext('%{size} MB', {
                size: (bytes / (1024 * 1024)).toFixed(2)
            });
        },
        openFileSelect() {
            this.$refs.files.click();
        },
        selectFiles() {
            this.selectedFiles = this.$refs.files.files;
        }
    }
}
</script>

<style lang="scss" scoped>
input[type=file] {
    display: none;
}
button {
    margin-top: 0;
    margin-bottom: 0;
    padding: 5px 10px;

    img {
        margin-right: 5px;
        vertical-align: text-bottom;
    }
}
.select {
    margin-right: 0;
}
.file-count {
    border: solid thin var(--light-gray-color-40);
    border-left: unset;
    display: inline-block;
    margin-left: -4px;
    padding: 5px 10px 4px 10px;
    position: relative;
    top: 2px;
}
.upload {
    margin-left: 15px;
}
</style>