I want to display the content of a file in a text area.
I use this script to do that but when I click in the open button the text doesn't appear:
function loadFileAsText()
{
document.getElementById("textoNormal").scrollTop=0;
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("textoNormal").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
id=1;
}
textoNormal: is the id of my text area
Read file of content in onChange event of File Upload Control.
function OnUpload() {
var obj = document.getElementById("<%=FleUldLogo.ClientID%>");
var source = obj.value;
var file = obj.files[0];
var textarea=$("#txtar");
var reader = new FileReader();
reader.onloadend = function () {
textarea.value= reader.result;
}
reader.readAsText(file);
return true;
}
I called your function in a jQuery onClick and it worked well,
jquery
$(document).ready(function(){
$('.clickMe').click(function(){
document.getElementById("textoNormal").scrollTop = 0;
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("textoNormal").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
id=1;
});
});
HTML
<textarea id="textoNormal"></textarea>
<input type="file" id="fileToLoad" />
<div class="clickMe">Click To Read Text File</div>
Related
I want to load an audio file via input file reader and then be able to play it ...
Here is the process of creating file blob and assign the audio source to this blob:
fileInput.on('change', readFile);
function readFile(e) {
const input = e.target;
if(input.files && input.files[0]) {
const reader = new FileReader();
reader.onload = (e) => {
const binaryData = e.target.result;
const binary = convertDataURIToBinary(binaryData);
const blob = new Blob(binary, {type: input.files[0].type });
console.log('inserting blobXX', blob);
recordedAudioComponent.src = URL.createObjectURL(blob);
data.instructAudios[component] = blob;
console.log('recordedAudioComponent.src', recordedAudioComponent.src);
}
reader.readAsDataURL(input.files[0]);
}
}
And here is the result of above code on console:
Now I'm trying to play the recordedAudioComponent which is an audio tag with the source right?
const recordedAudioComponent = document.getElementById(`recordedAudio-instruct-${component}`);
console.log(recordedAudioComponent)
recordedAudioComponent.play();
And here is the error it produces:
How can I fix this and play the audio?
EDIT: Here is the function to get the blob in the first code:
var BASE64_MARKER = ';base64,';
function convertDataURIToBinary(dataURI) {
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
You don't need any of convertion, blob is actually pretty similar from the file itself and the method URL.createObjectURL accepts file as argument, all you need to do to play the audio file from your browser is set the src attribute as the url created, something like this:
<body>
<input style="display: block;" type="file" onchange="loadAudioFile(this)">
<audio src=" " id="track" controls>
</audio>
<script>
function loadAudioFile(e) {
const url = URL.createObjectURL(e.files[0]);
document.getElementById('track').setAttribute('src', url);
}
</script>
</body>
<input type="file" name="fileInput" id="fileInput">
<script>
const fileInput = document.querySelector('#fileInput');
fileInput.onchange = readFile;
/** #type {AudioNode} */
const audio = document.createElement( 'audio' );
function readFile(e) {
const input = e.target;
if(input.files && input.files[0]) {
const reader = new FileReader();
reader.onloadend = function (e){
audio.src = e.target.result;
audio.play();
}
reader.readAsDataURL(input.files[0]);
}
}
</body>
I try to reset filefield after select file with the same name, but it not work:
onUploadPhonesCSV: function (field, value, opts) {
var inputEl = field.fileInputEl.dom,
fileList = inputEl.files;
var file = fileList[0];
var vm = this.getViewModel();
console.log(value);
if (field.isValid()) {
var reader = new FileReader();
reader.onload = function (oFREvent) {
var myCsv = oFREvent.target.result;
vm.set('phones', myCsv);
field.setRawValue("");
};
reader.readAsBinaryString(file);
}
},
Also tried field.reset() it not work.
Help me please.
I'm making my own rich text editor. So instead of inserting an image through URL, I'm doing through local files.
Iframe
<iframe style="width:100%; height: 300px;" name="richTextField"></iframe>
My Question is How to insert an image in an iframe?
function image()
{
$('#myModal').modal('show');
var file = document.getElementById("image").files;
$('#uploadImage').click(function(){
loadImageFileAsURL();
});
}
function loadImageFileAsURL()
{
var filesSelected = document.getElementById("image").files;
if (filesSelected.length > 0)
{
var fileToLoad = filesSelected[0];
if (fileToLoad.type.match("image.*"))
{
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var imageLoaded = document.createElement("img");
imageLoaded.src = fileLoadedEvent.target.result;
//How to insert Image??
$('#myModal').modal('hide');
};
fileReader.readAsDataURL(fileToLoad);
}
}
}
my code looks like this;
<form>
<input type="text" />
<input type="file">
</form>
<div id="notes"></div>
i got the text variables to work, however, i cannot get this silly image thing to work, i've looked at loads of tutorials but i simply cannot manage to do it
i know i have to do something with
(document.getElementById("file").files)[0] != null) {
var pic = (document.getElementById("file").files)[0];
var imgUrl;
var reader = new FileReader();
reader.onload = function(e) {
var imgURL = reader.result;
saveDataToLocalStorage(imgURL);
reader.readAsDataURL(pic);
for the image and then use the JSON.parse to get the url back and show it on the page
but i cannot figure out how it works, neither can i find any examples that aren't too complicated to implement it into my own code
in this fiddle i have provided all the code that i have at the moment
http://jsfiddle.net/VXdkC/
i really hope you guys can help me out, i've been messing around with this thing the past 2 days and it's starting to frustrate me :(
Here's how I'd do it :
var notes = localStorage.getItem('notes'),
arr = [];
if (notes) {
arr = JSON.parse(notes);
$.each(arr, function(k,v) {
console.log(v)
var h1 = $('<h1 />', {text: v.title});
var p = $('<p />', {text: v.msg});
var img = $('<img />', {src: v.image});
$('#notes').append(h1, p, img);
});
}
$('#clear').click(function () {
if (confirm('This will clear all notes, are you sure?')) {
window.localStorage.setItem('notes','');
location.reload();
}
return false;
});
$('#addNote').click(function () {
var Title = $('#title').val();
var Message = $('#message').val();
var file = $('#file').prop('files')[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
var b64 = e.target.result;
var note = {
image : b64,
title : Title,
msg : Message
}
arr.push(note);
localStorage.setItem('notes', JSON.stringify( arr ));
$('#notes').prepend("<div class='entry'><h1>" + Title + "</h1></p>" + "<p>" + Message + "<img src=" + b64 + " /></p> </div>");
}
return false;
});
FIDDLE
Its pretty simple
var pic = document.getElementById("file").files[0];
var imgUrl;
var reader = new FileReader();
reader.onload = function(e) {
var imgURL = reader.result;
$('#notes').prepend("<div class='entry'><h1>" + Title + "</h1></p>" + "<p>" + Message + "<img src=" + imgURL + "></p> </div>");
var notes = $('#notes').html();
localStorage.setItem('notes', notes);
saveDataToLocalStorage(imgURL);
}
reader.readAsDataURL(pic);
http://jsfiddle.net/VXdkC/2/
Live demo here (click).
the html:
<input id="file" type="file">
the js:
var fileInput = document.getElementById('file');
fileInput.addEventListener('change', function(e) {
var reader = new FileReader(); //create reader
reader.onload = function() { //attach onload
//do something with the result
console.log(reader.result);
localStorage.img = reader.result; //saved to localStorage
createImg(localStorage.img); //retrieved from localStorage
};
var file = e.target.files[0];
reader.readAsDataURL(file); //trigger onload function
});
function createImg(dataUri) {
var img = document.createElement('img');
img.src = dataUri;
document.body.appendChild(img);
}
In JQuery, How can i get image element from url?
For example,
<input type="file" id="fileInput"/>
var fileInput = document.getElementById('fileInput');
Here its is done via file picker in html5. But, how could i get image from just url?
My requirement is to get image from url.
var fileInput = document.getElementById('fileInput'); //instead of this how could i get element from url
var fileDisplayArea = document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var imageType = /image.*/;
if (file.type.match(imageType)) {
var reader = new FileReader();
//save to file api
} else {
fileDisplayArea.innerHTML = "File not supported!";
}
});
Please help,
Thanks
Do like that:
var fileInput = document.getElementById('fileInput'); //instead of this how could i get element from url
var fileDisplayArea = document.getElementById('fileDisplayArea');
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
var imageType = /image.*/;
if (file.type.match(imageType)) {
var reader = new FileReader();
reader.onload = (function(file){
return function(e){
// do something
}
})(file)
reader.readAsDataURL(file):
} else {
fileDisplayArea.innerHTML = "File not supported!";
}
});
Reading files in JavaScript using the File APIs