-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimgur.html
More file actions
131 lines (116 loc) · 3.07 KB
/
Copy pathimgur.html
File metadata and controls
131 lines (116 loc) · 3.07 KB
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
<!DOCTYPE html>
<html>
<head>
<title>IMGUR TEST</title>
<style>
.dragandrophandler {
border: 2px dotted #0B85A1;
width: 400px;
color: #92AAB0;
text-align: left;
vertical-align: middle;
padding: 10px 10px 10 10px;
margin-bottom: 10px;
font-size: 200%;
}
.inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
.inputLabel {
font-size: 1.25em;
font-weight: 700;
color: white;
background-color: black;
display: inline-block;
}
.formLabel {
width: inherit;
height: inherit;
text-align: center;
}
.inputFile:focus+label,
.inputLabel:hover {
background-color: red;
}
</style>
</head>
<body>
<form class="dragandrophandler">
<input id="uploadBtn" name="uploadBtn" type="file" class="upload inputfile">
<label for="uploadBtn" class="inputLabel formLabel">
Add Images
</label>
</form>
<input id="uploadFile" placeholder="0 files selected" disabled="disabled">
<hr>
<img id="displayImage" src="">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
var imgur_client_id = "cffdbdcf9cb88c7";
$(".dragandrophandler").on('dragenter', function(e) {
e.stopPropagation();
e.preventDefault();
$(this).css('border', '2px solid #0B85A1');
});
$(".dragandrophandler").on('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
});
$(".dragandrophandler").on('drop', function(e) {
$(this).css('border', '2px dotted #0B85A1');
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
//We need to send dropped files to Server
console.log(files);
for (var i = 0; i < files.length; i++) {
readFile(files[i]);
}
});
function readFile(file) {
var fileType = file["type"].split("/")[0];
if (fileType !== "image") {
console.log("Not an image")
} else {
console.log(file);
var reader = new FileReader();
reader.onloadend = function() {
$("#displayImage").attr("src", reader.result);
var base64result = reader.result.split(',')[1];
var form = new FormData();
form.append("image", base64result);
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.imgur.com/3/image",
"method": "POST",
"headers": {
"authorization": `Client-ID ${imgur_client_id}`
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function(response) {
var res = JSON.parse(response);
console.log(res.data.link);
});
}
reader.readAsDataURL(file);
}
}
document.getElementById("uploadBtn").onchange = function (event) {
document.getElementById("uploadFile").value = this.value;
console.log(event);
console.log(this.value);
var file = this.files[0];
readFile(file);
};
</script>
</body>
</html>