1

I am trying to read in a local sound file and return it using FileReader but FileReader is undefined. Here is my code:

//play a sound
router.get('/sound/:filename', function(req, res, next) {
    var reader = new FileReader()
    reader.onload = function(e) {
        var arrayBuffer = reader.result;
    }
    reader.readAsArrayBuffer(req.params.filename)
    res.send(arrayBuffer)
})

This is inside my api file written purely in Javascript. If I can't use FileReader, is there another way of getting a local file and returning it? Thanks.

theboneman14
  • 41
  • 1
  • 8
  • Which browser are you testing? – Narendra Jan 02 '19 at 04:39
  • Are you running this code on the client or server? `router.get` looks like a server function. – Barmar Jan 02 '19 at 04:43
  • it looks like you are using nodejs. in node there is a separate module for file operations called ```fs```. you need to import that and use ```fs``` module to read your file – Saad Mehmood Jan 02 '19 at 04:55
  • The FileReader will work when you are trying to upload a file from client side or in your web application. An example will be that you can use it in a callback when the file is uploaded on an input element. However, as Barmar pointed, this appears as a server code and won't work here. – shmit Jan 02 '19 at 04:56
  • Hello, I'm using Chrome, running on server. I'll try fs. Thanks everyone. – theboneman14 Jan 02 '19 at 05:04

0 Answers0