I want to read a file from local storage and upload it via ajax. How is this done?
Asked
Active
Viewed 62 times
0
-
Not sure if it's what you're asking for, but have you considered `` (http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously?rq=1)? Web pages can't otherwise read local storage because they are sandboxed AFAIK. – CoconutFred Aug 16 '16 at 22:21
2 Answers
0
In most browsers, you can use FileReader
to read data from file inputs. There are various functions for reading the data; this example uses the function that returns an ArrayBuffer
containing all the bytes:
<script>
window.onload = function() {
document.getElementById('upload').onchange = function(e) {
var file = e.target.files[0];
var fileReader = new FileReader();
fileReader.onload = function(e) {
var bytes = e.target.result;
console.log(bytes);
};
fileReader.readAsArrayBuffer(file);
};
};
</script>
<input type = 'file' id = 'upload' />

csander
- 1,385
- 10
- 11
0
I managed to figure it out. Here's the code for anyone interested.
var form = new FormData();
form.append("data", angular.toJson(message));
var bytes = new Uint8Array(audio.length); //audio is an IBuffer
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(audio);
dataReader.readBytes(bytes);
dataReader.close();
var media = new Blob([bytes], { type: "application/octet-stream" }); //application/octet-stream or audio/mpeg?
form.append("attached_files", media, "recording-aac.caf");
return $http.post(AppSettings.baseUri + "api/sendmessage", form, { headers: { "Content-Type": undefined } });

Matt
- 6,264
- 10
- 54
- 82