0

I am building an endpoint on my ExpressJS server to download an APK file given the correct credentials. Here is what my server endpoint looks like:

app.post('/App/download',  (req, res) => {
    //Check if password is correct
    if(req.body.key == 'xxxxx') {        
        res.sendFile(path.join(__dirname, '..', '/server', '/AuthorizedClientResources', 'app.apk'), err => {
            if(err)
                console.log('Error is:\n', err)
        })
    } else {
       //Any suggestions for invalid password handling would be appreciated
       res.sendStatus(401)
    }
})

And here is what my post request looks like on the client-side:

try {
    axios.post('http://domainName/App/download', { key: this.appKey })
        .then(response => { 
            console.log('Response was:\n', response) 
        })
        .catch(err => (console.log('The error was:\n', err)))
} catch(error) {
    alert(`You entered incorrect credentials.`)
}

When a response is received (given correct credentials) the file is sent and the response body looks like this using the Chrome developer tools. The file is 7.1MB on my file system and it is sent in the response as ~13MB. File size

Is there any reason for this?

Any help is appreciated. Thank you!

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
  • Why is the response a string? That doesn't look right. – ASDFGerte Dec 09 '20 at 19:19
  • 1
    Your file is not enlarged. It’s because you print the binary out as string, and the meaningless string representation is larger. You should inspect in the network panel of devtool instead. – hackape Dec 09 '20 at 19:26
  • Does this answer your question? [download file using an ajax request](https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request) – Lawrence Cherone Dec 09 '20 at 19:27
  • I will look into your answer @LawrenceCherone. I have not used AJAX with javascript yet so I will need to familiarize myself with that concept. I was not aware that the data being sent was anything other than the file being sent. Is there a way that I can access the file binary from the response? – Prowtons McBishop Dec 09 '20 at 19:50
  • 1
    axios.post is AJAX, which is why you're getting that response and not a prompt to download – Lawrence Cherone Dec 09 '20 at 20:10
  • @LawrenceCherone Would you be able to point me towards any resources (or perhaps provide an answer directly if possible) that would allow me to store the binary in a variable? This is ultimately my end goal on the client side and my immediate issue at the moment. – Prowtons McBishop Dec 09 '20 at 20:19
  • 1
    Store in a variable? The linked answer shows how to download https://stackoverflow.com/a/42815974/661872 – Lawrence Cherone Dec 09 '20 at 20:27
  • @LawrenceCherone I think that you may have answered my question. Is there a way for the server to trigger a download if the password is correct and send an unauthorized response if the password is false all in one request or would I need to make two requests for this? One to verify password and one to download the file if the password is correct. I apologize for my ignorance on this topic. I'm not too familiar with HTTP. – Prowtons McBishop Dec 09 '20 at 20:35
  • 1
    respond with json, `res.status(401).json({error: 'You entered incorrect credentials'})` then handle it in axios where `The error was:\n` is – Lawrence Cherone Dec 09 '20 at 20:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225746/discussion-between-prowtons-mcbishop-and-lawrence-cherone). – Prowtons McBishop Dec 09 '20 at 20:43

0 Answers0