Sukhpal Saini
Sukhpal Saini

Reputation: 11

HttpRequestHandler : Sending a pdf file in response

I want server a pdf file instead of mp3 file please help

private HttpRequestHandler mRequestHandler = new HttpRequestHandler()
    {

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException 
            {
            try{
                File file = new File("/mnt/sdcard/Music/song.mp3");
                FileEntity body = new FileEntity(file, "audio/mpeg");
                response.setHeader("Content-Type", "application/force-download");
                response.setHeader("Content-Disposition","attachment; filename=song.mp3");
                response.setEntity(body);

            }catch(Exception e)
            {
                e.printStackTrace();
            }

            }

    };

Upvotes: 0

Views: 95

Answers (1)

Aschab
Aschab

Reputation: 1399

Change the content type to application/pdf

    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext context)
            throws HttpException, IOException 
        {
        try{
            File file = new File("/my/path/file.pdf");
            FileEntity body = new FileEntity(file, "application/pdf");
            response.setHeader("Content-Type", "application/force-download");
            response.setHeader("Content-Disposition","attachment; filename=file.pdf");
            response.setEntity(body);

        }catch(Exception e)
        {
            e.printStackTrace();
        }

        }

};

you can check mime types here

Upvotes: 1

Related Questions