This gist was generated by the Svelte REPL. Visit https://svelte.technology/repl?gist=this_gist_id to see it.
Created
March 20, 2018 12:56
-
-
Save anonymous/59aa9eb0e20dc1f0271a314aa16159ac to your computer and use it in GitHub Desktop.
Svelte component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>File reader API example</h1> | |
<p>taken from an example from <a href="https://alligator.io/vuejs/file-reader-component/"> alligator</a></p> | |
<textarea rows=5 bind:value="text"></textarea> | |
<FileReader on:load="set({'text':event.content})"/> | |
<script> | |
import FileReader from './FileReader.html' | |
export default { | |
components: {FileReader} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"text": "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label class="text-reader"> | |
Read file | |
<input type="file" on:change="loadTextFromFile(event)"> | |
</label> | |
<script> | |
export default { | |
methods: { | |
loadTextFromFile(ev) { | |
const file = ev.target.files[0] | |
const reader = new FileReader() | |
reader.onload = e => (this.fire('load',{content:e.target.result})) | |
reader.readAsText(file) | |
} | |
} | |
} | |
</script> | |
<style> | |
.text-reader { | |
position: relative; | |
overflow: hidden; | |
display: inline-block; | |
border: 2px solid black; | |
border-radius: 5px; | |
padding: 8px 12px; | |
cursor: pointer; | |
} | |
.text-reader input { | |
position: absolute; | |
top: 0; | |
left: 0; | |
z-index: -1; | |
opacity: 0; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment