3

In my Grails applications, I have a controller function to return a PDF file.

When I make a call to the URL (to return the file), It downloads the file, rather than displaying the PDF file in the browser.

When I open other pdf files from other websites, it displays in browser.. so I think it has something to do with my returned response?

def separator = grailsApplication.config.project.separator.flag
def path = grailsApplication.config.project.images.path+"public/"+user.id+"/"
render(contentType: "multipart/form-data", file: new File(path+note.preview), fileName: note.preview)

Do I need to change the contentType? (I kind of tried to make it /application/pdf but didnt work?..still downloads.

Tyler Evans
  • 567
  • 1
  • 8
  • 25

2 Answers2

4

Try setting content-disposition to inline. Content-Type tells the browser what type of content it is but the disposition tells the browser on how to handle it.

More information in this answer

Community
  • 1
  • 1
Praba
  • 1,373
  • 10
  • 30
  • how do i set the content disposition.. when I try response.setHeader and then call render after, i think it overrides it? – Tyler Evans Sep 11 '14 at 13:14
  • 1
    Can you directly write to the outputstream? Something like: `response.outputStream << your_file response.outputStream.flush()` – Praba Sep 11 '14 at 13:19
  • I've tried the following with no luck ---> response.setHeader "Content-disposition", "inline; filename="+note.preview response.contentType = 'application/pdf' response.outputStream << new File(path+note.preview) response.outputStream.flush() – Tyler Evans Sep 11 '14 at 13:30
  • I get "Failed to load document" so it seems its fetching the file correctly.. but just can't display it properly – Tyler Evans Sep 11 '14 at 13:34
  • You sure about the content of the PDF file? It opens in Adobe reader when it's sent as an attachment? May be try a different browser? – Praba Sep 11 '14 at 13:35
  • I found if I make it "attachment" instead of "inline"... it downloads the attachment.. but when i try open that up.. it says its corrupt.. so it must be the way im returning the output? – Tyler Evans Sep 11 '14 at 13:37
  • If the answer helped you, please mark it as accepted. Also, what did you do to fix the 'Failed to load' error? can you update your question with that? – Praba Sep 11 '14 at 13:41
1

There was something getting corrupt by returning a "File" object rather than a byte[] object.

So I added the following lines.

byte[] DocContent = null;
DocContent = getFileBytes(path+note.preview);

response.setHeader "Content-disposition", "inline; filename="+note.preview+""
response.contentType = 'application/pdf'
response.outputStream << DocContent
response.outputStream.flush()


public static byte[] getFileBytes(String fileName) throws IOException
{
    ByteArrayOutputStream ous = null;
    InputStream ios = null;
    try
    {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(new File(fileName));
        int read = 0;
        while ((read = ios.read(buffer)) != -1)
            ous.write(buffer, 0, read);
    }
    finally
    {
        try
        {
            if (ous != null)
                ous.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
        try
        {
            if (ios != null)
                ios.close();
        }
        catch (IOException e)
        {
            // swallow, since not that important
        }
    }
    return ous.toByteArray();
}
Tyler Evans
  • 567
  • 1
  • 8
  • 25