-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexample.html
72 lines (65 loc) · 2.09 KB
/
example.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example form upload</title>
<script src="//code.jquery.com/jquery-1.12.2.min.js"></script>
</head>
<body>
<form>
<p>
<input type="file" name="file" id="file">
<input type="submit" value="Upload">
</p>
<canvas width="200" height="200" id="preview-canvas"></canvas>
</form>
<script src="node_modules/exif-js/exif.js"></script>
<script src="canvas-image-uploader.js"></script>
<script>
var uploader = new CanvasImageUploader({
maxSize: 1500,
jpegQuality: 0.7
});
var MAX_PREVIEW_SIZE = 200;
var file; // File to upload
var $previewCanvas = $('#preview-canvas');
function onImageChanged(e) {
var files = e.target.files || e.dataTransfer.files;
if (files) {
file = files[0];
var $canvas = $('<canvas>');
uploader.readImageToCanvas(file, $canvas, function () {
uploader.copyToCanvas($canvas[0], $previewCanvas, MAX_PREVIEW_SIZE); // Copy to preview
uploader.saveCanvasToImageData($canvas[0]); // Save for later use in uploader.getImageData()
});
}
}
function getFileExtension(filename) {
return filename.split('.').pop();
}
// Upload the file data
function onSubmit(e) {
e.preventDefault();
if (file) {
$.ajax({
type: 'POST',
url: 'http://...',
data: uploader.getImageData(), // Get saved image data, from uploader.saveCanvasToImageData()
beforeSend: function (request) {
request.setRequestHeader("Content-Type", getFileExtension(file.name));
},
processData: false,
success: function (result) {
},
error: function (error) {
}
});
} else {
console.warn("No file selected...");
}
}
$('#file').bind('change', onImageChanged);
$('form').on('submit', onSubmit);
</script>
</body>
</html>