1
1
var database = firebase . database ( ) ;
2
+
3
+ function upload_project_image ( ) {
4
+ var file = document . getElementById ( 'image' ) . files [ 0 ]
5
+ console . log ( file )
6
+ var filename = file . name
7
+ console . log ( filename )
8
+ // Create a reference to the image
9
+ var storageRef = firebase . storage ( ) . ref ( ) ;
10
+ var projectImageRef = storageRef . child ( 'projectImages/' + filename ) ;
11
+
12
+ var uploadImage = projectImageRef . put ( file ) ;
13
+ uploadImage . on ( 'state_changed' , function ( snapshot ) {
14
+ // Observe state change events such as progress, pause, and resume
15
+ // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
16
+ var progress = ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 ;
17
+ console . log ( 'Upload is ' + progress + '% done' ) ;
18
+ switch ( snapshot . state ) {
19
+ case firebase . storage . TaskState . PAUSED : // or 'paused'
20
+ console . log ( 'Upload is paused' ) ;
21
+ break ;
22
+ case firebase . storage . TaskState . RUNNING : // or 'running'
23
+ console . log ( 'Upload is running' ) ;
24
+ break ;
25
+ }
26
+ } , function ( error ) {
27
+ // Handle unsuccessful uploads
28
+ } , function ( ) {
29
+ // Handle successful uploads on complete
30
+ // For instance, get the download URL: https://firebasestorage.googleapis.com/...
31
+ uploadImage . snapshot . ref . getDownloadURL ( ) . then ( function ( downloadURL ) {
32
+ console . log ( 'File available at' , downloadURL ) ;
33
+ return downloadURL
34
+ } ) ;
35
+ } ) ;
36
+
37
+ }
38
+
39
+
2
40
function submitInfo ( ) {
3
41
4
42
if ( currentUid == null ) {
@@ -10,7 +48,7 @@ function submitInfo() {
10
48
var lookFor = document . getElementById ( "lookFor" ) . value ;
11
49
var projectDetails = document . getElementById ( "projectDetails" ) . value ;
12
50
var projectType = document . getElementById ( "projectType" ) . value ;
13
- var image = document . getElementById ( "image" ) . value ;
51
+
14
52
var verificationNumber = document . getElementById ( "verificationNumber" ) . value ;
15
53
var createdBy = currentUid ;
16
54
var groupSize = document . getElementById ( "groupSize" ) . value ;
@@ -104,7 +142,41 @@ function submitInfo() {
104
142
105
143
}
106
144
107
- firebase . database ( ) . ref ( 'v2/projectDrafts/' ) . push ( ) . set ( mapswipe_import )
145
+ var file = document . getElementById ( 'image' ) . files [ 0 ]
146
+ console . log ( file )
147
+ var filename = file . name
148
+ console . log ( filename )
149
+ // Create a reference to the image
150
+ var storageRef = firebase . storage ( ) . ref ( ) ;
151
+ var projectImageRef = storageRef . child ( 'projectImages/' + filename ) ;
152
+
153
+ var uploadImage = projectImageRef . put ( file ) ;
154
+ uploadImage . on ( 'state_changed' , function ( snapshot ) {
155
+ // Observe state change events such as progress, pause, and resume
156
+ // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
157
+ var progress = ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 ;
158
+ console . log ( 'Upload is ' + progress + '% done' ) ;
159
+ switch ( snapshot . state ) {
160
+ case firebase . storage . TaskState . PAUSED : // or 'paused'
161
+ console . log ( 'Upload is paused' ) ;
162
+ break ;
163
+ case firebase . storage . TaskState . RUNNING : // or 'running'
164
+ console . log ( 'Upload is running' ) ;
165
+ break ;
166
+ }
167
+ } , function ( error ) {
168
+ // Handle unsuccessful uploads
169
+ } , function ( ) {
170
+ // Handle successful uploads on complete
171
+ // For instance, get the download URL: https://firebasestorage.googleapis.com/...
172
+ uploadImage . snapshot . ref . getDownloadURL ( ) . then ( function ( downloadURL ) {
173
+ console . log ( 'File available at' , downloadURL ) ;
174
+ mapswipe_import . image = downloadURL
175
+ console . log ( mapswipe_import )
176
+
177
+ // upload projectDraft to firebase once image has been uploaded
178
+
179
+ firebase . database ( ) . ref ( 'v2/projectDrafts/' ) . push ( ) . set ( mapswipe_import )
108
180
. then ( function ( ) {
109
181
clear_all_fields ( ) ;
110
182
displaySuccessMessage ( ) ;
@@ -113,5 +185,9 @@ function submitInfo() {
113
185
alert ( 'could not upload data: ' + error ) ;
114
186
} ) ;
115
187
188
+
189
+
190
+ } ) ;
191
+ } ) ;
116
192
}
117
193
}
0 commit comments