Skip to content

Commit 181bf76

Browse files
committed
filereader
1 parent df7364b commit 181bf76

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

javascript-filereader/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# JavaScript FileReader
2+
3+
> [https://www.youtube.com/watch?v=-AR-6X_98rM](https://www.youtube.com/watch?v=-AR-6X_98rM)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `npm start` to run the app viewable on `http://localhost:9966`.

javascript-filereader/bear.jpg

58.2 KB
Loading

javascript-filereader/bears.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name,type
2+
tim,grizzly
3+
randy,brown

javascript-filereader/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>JS FileReader</title>
6+
</head>
7+
<body>
8+
9+
<input type="file" />
10+
11+
<script src="bundle.js" charset="utf-8"></script>
12+
</body>
13+
</html>

javascript-filereader/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const input = document.querySelector('input[type="file"]')
2+
function handleFiles (files) {
3+
console.log(files)
4+
const reader = new FileReader()
5+
reader.onload = function () {
6+
// const lines = reader.result.split('\n').map(function (line) {
7+
// return line.split(',')
8+
// })
9+
// console.log(lines)
10+
const img = new Image()
11+
img.onload = function () {
12+
const canvas = document.createElement('canvas')
13+
const context = canvas.getContext('2d')
14+
context.drawImage(img, 0, 0)
15+
16+
const imageData = context.getImageData(0, 0, canvas.width, canvas.height)
17+
const data = imageData.data
18+
for (var i = 0; i <= data.length; i += 4) {
19+
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3
20+
data[i] = avg
21+
data[i + 1] = avg
22+
data[i + 2] = avg
23+
}
24+
context.putImageData(imageData, 0, 0)
25+
26+
document.body.appendChild(canvas)
27+
//canvas.toDataURL()
28+
//const csvfile = new Blob(['one,two,three'], { type: 'text/csv' })
29+
canvas.toBlob(function (blob) {
30+
const form = new FormData()
31+
form.append('image', blob, 'moody.jpg')
32+
const xhr = new XMLHttpRequest()
33+
xhr.open('POST', '/imageupload', true)
34+
xhr.send(form)
35+
})
36+
}
37+
img.src = reader.result
38+
//document.body.appendChild(img)
39+
}
40+
//reader.readAsText(files[0])
41+
reader.readAsDataURL(files[0])
42+
}
43+
44+
input.addEventListener('change', function (e) {
45+
handleFiles(input.files)
46+
}, false)
47+
48+
document.addEventListener('dragover', function (e) {
49+
e.preventDefault()
50+
e.stopPropagation()
51+
}, false)
52+
document.addEventListener('drop', function (e) {
53+
e.preventDefault()
54+
e.stopPropagation()
55+
handleFiles(e.dataTransfer.files)
56+
}, false)

javascript-filereader/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "javascript-filereader",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js:bundle.js"
8+
},
9+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"budo": "^11.2.2"
13+
}
14+
}

0 commit comments

Comments
 (0)