Skip to content Skip to sidebar Skip to footer

Streaming Bytes From Controller, Android Browser, Download Fails

Grails 1.3.7 I have some code that looks like this... response.setHeader('Content-disposition', 'attachment; filename=${fileName}') response.contentType = download.contentType resp

Solution 1:

I realize this is a few months late but I also ran into this issue with the Android browser and a Grails application I was working on.

The issue appears to be how Android handles downloadable content and the android browser integration with download manager.

http://code.google.com/p/android/issues/detail?id=1978

http://code.google.com/p/android/issues/detail?id=18462

I was receiving two requests on the server side for a downloadable file; one from the browser and one from the download manager. The one from the browser ends up getting terminated and the socket closed as soon as the browser determines that it is downloadable content. The browser then hands off the download to the download manager.

I was also having issues with the download failing from download manager but that had to due with me not sending headers as soon as they were ready. I ran into this only with larger APKs, small APKs (under 10-20K) seemed to download just fine.

Some code may help:

    response.contentType = 'application/vnd.android.package-archive'
    response.addHeader('Content-Disposition', 'attachment; filename=FILENAME.APK')

    // output file content
    response.setStatus(200)
    response.setContentLength("CONTENTSIZE")

    // send headers
    response.flushBuffer()

    try {
        response.outputStream << {FILE}.getBytes()
        response.outputStream.flush()
    } catch (SocketException e) {
        log.error(e)
    }
    return

With this, I always end up with one socket exception. Don't know if thats avoidable, from some quick searching I didn't see a way to determine socket state from servlet without simply trying to write to the socket.


Post a Comment for "Streaming Bytes From Controller, Android Browser, Download Fails"