Results 1 to 2 of 2

Thread: Downloading PDF files - Does not open file in browser

  1. #1

    Default Downloading PDF files - Does not open file in browser

    clicking on a link for a PDF file just opens a blank page on the browser. If I save the file to a directory and then open it, the file opens without any problem. The exact code works correct outside of Spring framework using a plain servet. Any ideas what needs to change to make it work in Spring?

    Here is the code:
    Code:
    public ModelAndView binitemHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String repId = RequestUtils.getStringParameter(request, "RepID", "");
    		BinaryItem binitem = null;
    		ModelAndView mav = null;
    		String ext = "";
    
    		
    			binitem = (BinaryItem) myFacade.getBinItem(repId);
    
    			fileName = binitem.getFilename();
    			logger.debug("Filename is: "+fileName);
    			if (fileName != null) {
    				int dot = fileName.lastIndexOf('.');
    				ext = (dot > 0) ? fileName.substring(dot) : "txt";
    				logger.debug("Extension is: "+ext);
    			}
    			response.setContentType(mapFileToMime(ext));
    			logger.debug("Extension is setContentType: "+mapFileToMime(ext));
    			response.setHeader("Content-disposition", "inline; filename=" + fileName);
    			FileCopyUtils.copy(binitem.getBytes(), response.getOutputStream());
    
    		return mav;
    }

    Code:
    private String mapFileToMime(String type) {
    		if (type.equalsIgnoreCase(".gif")) {
    			return "image/gif";
    		} else if (type.equalsIgnoreCase(".jpg")) {
    			return "image/jpg";
    		} else if (type.equalsIgnoreCase(".jpeg")) {
    			return "image/jpg";
    		} else if (type.equalsIgnoreCase(".doc")) {
    			return "application/msword";
    		} else if (type.equalsIgnoreCase(".123")) {
    			return "application/vnd.lotus-1-2-3";
    		} else if (type.equalsIgnoreCase(".prz")) {
    			return "application/vnd.lotus-freelance";
    		} else if (type.equalsIgnoreCase(".lwp")) {
    			return "application/vnd.lotus-wordpro";
    		} else if (type.equalsIgnoreCase(".xls")) {
    			return "application/vnd.ms-excel";
    		} else if (type.equalsIgnoreCase(".ppt")) {
    			return "application/vnd.ms-powerpoint";
    		} else if (type.equalsIgnoreCase(".vsd")) {
    			return "application/vnd.visio";
    		} else if (type.equalsIgnoreCase(".pdf")) {
    			return "application/pdf";
    		} else if (type.equalsIgnoreCase(".wk4")) {
    			return "application/vnd.lotus-1-2-3";
    		}
    		return "application/octet-stream";
    }

  2. #2

    Default

    Well, this is how I hacked around the problem since it seems to be an IE issue requiring the .pdf extension in the URI.

    Code:
    public ModelAndView binitemHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String repId = RequestUtils.getStringParameter(request, "RepID", "");
    		String withHTML = RequestUtils.getStringParameter(request, "withHTML", "0");
    		String fileName = RequestUtils.getStringParameter(request, "Filename", "");
    		BinaryItem binitem = null;
    		ModelAndView mav = null;
    		String ext = "";
    
    			binitem = (BinaryItem) myFacade.getBinItem(repId);
    
    			fileName = binitem.getFilename();
    			if (fileName.endsWith(".pdf") && !request.getRequestURI().endsWith(".pdf")) {
    				response.sendRedirect(request.getContextPath()+"/binitem.pdf?" + request.getQueryString());
    			} else {
    				if (fileName != null) {
    					int dot = fileName.lastIndexOf('.');
    					ext = (dot > 0) ? fileName.substring(dot) : "txt";
    				}
    				response.setContentType(mapFileToMime(ext));
    				response.setHeader("Content-disposition", "inline; filename=" + fileName);
    				response.setContentLength(binitem.getBytes().length);
    				FileCopyUtils.copy(binitem.getBytes(), response.getOutputStream());
    			}
    
    		return mav;
    }

Similar Threads

  1. Replies: 5
    Last Post: Mar 17th, 2010, 04:32 AM
  2. Replies: 2
    Last Post: Jan 20th, 2009, 04:33 AM
  3. Saving large files to a db using hibernate?
    By Dan Washusen in forum Data
    Replies: 10
    Last Post: Sep 20th, 2006, 12:18 PM
  4. Replies: 0
    Last Post: Aug 9th, 2005, 11:49 AM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •