File tree Expand file tree Collapse file tree 3 files changed +33
-14
lines changed Expand file tree Collapse file tree 3 files changed +33
-14
lines changed Original file line number Diff line number Diff line change 1
1
<template >
2
2
<form @submit.prevent =" uploadFile" >
3
- <BaseInput
4
- type =" file"
5
- :label =" label"
6
- name =" file"
7
- v-model =" file"
8
- :accept =" fileTypes"
9
- class =" mb-4"
10
- />
11
- <div >
12
- <BaseBtn text =" Upload" />
3
+ <div class =" mb-4" >
4
+ <label for =" file" class =" sr-only" >
5
+ {{ label }}
6
+ </label >
7
+ <input
8
+ type =" file"
9
+ :accept =" fileTypes"
10
+ @change =" fileChange"
11
+ id =" file"
12
+ class =" appearance-none"
13
+ />
13
14
</div >
14
- <FlashMessage :error =" error" />
15
+ <BaseBtn text =" Upload" />
16
+ <FlashMessage :message =" message" :error =" error" />
15
17
</form >
16
18
</template >
17
19
18
20
<script >
19
21
import { getError } from " @/utils/helpers" ;
20
22
import BaseBtn from " @/components/BaseBtn" ;
21
- import BaseInput from " @/components/BaseInput" ;
22
23
import FileService from " @/services/FileService" ;
23
24
import FlashMessage from " @/components/FlashMessage" ;
24
25
@@ -29,14 +30,17 @@ export default {
29
30
type: Array ,
30
31
default: null ,
31
32
},
33
+ endpoint: {
34
+ type: String ,
35
+ required: true ,
36
+ },
32
37
label: {
33
38
type: String ,
34
39
default: " " ,
35
40
},
36
41
},
37
42
components: {
38
43
BaseBtn,
39
- BaseInput,
40
44
FlashMessage,
41
45
},
42
46
data () {
@@ -47,8 +51,21 @@ export default {
47
51
};
48
52
},
49
53
methods: {
54
+ clearMessage () {
55
+ this .error = null ;
56
+ this .message = null ;
57
+ },
58
+ fileChange (event ) {
59
+ this .clearMessage ();
60
+ this .file = event .target .files [0 ];
61
+ },
50
62
uploadFile () {
51
63
const payload = {};
64
+ const formData = new FormData ();
65
+ formData .append (" file" , this .file );
66
+ payload .file = formData;
67
+ payload .endpoint = this .endpoint ;
68
+ this .clearMessage ();
52
69
FileService .uploadFile (payload)
53
70
.then (() => (this .message = " File uploaded." ))
54
71
.catch ((error ) => (this .error = getError (error)));
Original file line number Diff line number Diff line change @@ -2,6 +2,6 @@ import * as API from "@/services/API";
2
2
3
3
export default {
4
4
async uploadFile ( payload ) {
5
- await API . apiClient . post ( "" , payload ) ;
5
+ await API . apiClient . post ( payload . endpoint , payload . file ) ;
6
6
} ,
7
7
} ;
Original file line number Diff line number Diff line change 6
6
<UpdatePassword class =" p-5 bg-white border rounded shadow" />
7
7
<FileUpload
8
8
label =" Upload Avatar"
9
+ :fileTypes =" ['image/*']"
10
+ endpoint =" /users/auth/avatar"
9
11
class =" p-5 bg-white border rounded shadow"
10
12
/>
11
13
</div >
You can’t perform that action at this time.
0 commit comments