-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.js
83 lines (72 loc) · 2.66 KB
/
upload.js
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
'use strict';
//https://css-tricks.com/drag-and-drop-file-uploading/
// feature detection for drag&drop upload
var isAdvancedUpload = function() {
var div = document.createElement('div');
return ( ( 'draggable' in div ) || ( 'ondragstart' in div && 'ondrop' in div ) ) && 'FormData' in window && 'FileReader' in window;
}();
function setupFileUpload() {
var theForm = $('#uploadForm');
var droppedFiles = false;
// automatically submit the form on file select
$('#file').on('change', function(e) {
submitForm();
});
// drag&drop files if the feature is available
if(isAdvancedUpload) {
theForm.addClass('has-advanced-upload' ); // letting the CSS part to know drag&drop is supported by the browser
theForm.on('drag dragstart dragend dragover dragenter dragleave drop', function(e) {
// preventing the unwanted behaviours
e.preventDefault();
e.stopPropagation();
});
theForm.on('dragover dragenter', function() {
theForm.addClass('is-dragover');
});
theForm.on('dragleave dragend drop', function() {
theForm.removeClass('is-dragover');
});
theForm.on('drop', function(e) {
droppedFiles = e.originalEvent.dataTransfer.files; // the files that were dropped
submitForm(); // automatically submit the form on file drop
});
} else {
$('#uploadForm').removeClass('is-uploading');
$('#uploadForm').addClass('is-error');
$('#uploadForm .box__error span').text("File upload requires JS to work.");
}
// if the form was submitted
var submitForm = function() {
// preventing the duplicate submissions if the current one is in progress
if(theForm.hasClass('is-uploading')) return false;
theForm.addClass('is-uploading').removeClass('is-error');
// gathering the form data
var files = $('#file')[0].files;
if(droppedFiles) files = droppedFiles;
uploadPic(files);
};
// Firefox focus bug fix for file input
$('#file').on('focus', function(){ $('#file').addClass( 'has-focus' ); });
$('#file').on('blur', function(){ $('#file').removeClass( 'has-focus' ); });
};
function uploadedPhotoError(err) {
console.log('There was an error uploading your photo: ', err.message);
$('#uploadForm').removeClass('is-uploading');
$('#uploadForm').addClass('is-error');
$('#uploadForm .box__error span').text(err.message);
}
function uploadedPhotoSuccess(data) {
console.log("Success",data);
$('#uploadForm').removeClass('is-uploading');
$('#uploadForm').addClass('is-success');
$('#file').off();
$('#uploadForm').off();
$('#photo').attr('src',data.Location);
setTimeout(function() {
$('.box__input').hide();
$('.box__success').hide();
$('#photo').addClass('uploaded');
$('#uploadForm').addClass('uploaded');
$('#details').show();
},1000);
}