Results 1 to 4 of 4

Thread: Problem downloading file from db

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    2

    Default Problem downloading file from db

    Hello there, i've browsed the forum a lot about this problem, but couldn't find any clear solution:

    I'm trying to download a file (not showing it) directly from a oracle database by clicking on a link in a page managed by a child class of SimpleFormController.

    Here is the code i use to pass the file to the jsp:

    Code:
    protected ModelAndView processFormSubmission(
                final HttpServletRequest request,
                final HttpServletResponse response,
                final Object command,
                final BindException errors) throws Exception {
            
            //Retreives the id sent by the jsp after clicking on the link to download
            Long fileToDownloadId = RequestUtils.getLongParameter(request, "download");
    
            if(fileToDownloadId != null && fileToDownloadId .longValue() > 0){
                //retreives the image object containing the file
                MyImageObject obj = mySecurityManager.getMyImageObject();
                String filename = obj.getFileName();
                byte[] fileBytes = obj.getFileBytes(); 
    
                response.setContentType("application/cer");
                response.setHeader("Content-Disposition","attachment; filename=\""+filename +"\"");
    
                ServletOutputStream outs = response.getOutputStream()
                outs.write(fileBytes);
                outs.flush();
                outs.close();
                return showForm(request, response, errors);
            }
            //more code
        }
    Two problems arise:

    1) I manage to fetch the file as a byte array just fine, and after clicking on the link, i do have a download popup appearing, however I'm downloading the entire html page in addition of the file's content! The downloaded file starts with the data of the file fetched in db, followed by the html page's data.

    2) In IE only, the name of the downloaded file is the the name of the page (myJsp.do instead of myFilename.cer).

    I'd like of course to get the fetched file's data only.

  2. #2
    Join Date
    Dec 2006
    Location
    Normal, Illinois
    Posts
    277

    Default

    Check out the solution I provided on this thread.

    http://forum.springframework.org/showthread.php?t=33118

    Let me know if that helps resolve your issues.
    Caleb Washburn

  3. #3
    Join Date
    Aug 2005
    Location
    Boston, United States
    Posts
    45

    Default

    You should not return "showForm(request, response, errors)". This is showing the html. When writing to the output directly, you should return null. See the imagedb example project from spring: ImageController

    The show image action looks like this:
    Code:
    public ModelAndView streamImageContent(HttpServletRequest request, HttpServletResponse response) throws Exception {
         		this.imageDatabase.streamImage(request.getParameter("name"), response.getOutputStream());
        		return null;
        	}

  4. #4
    Join Date
    Jan 2007
    Posts
    2

    Default

    Thancks a lot for the help, the return null did the trick. I had tried it at some point, but wasn't using the right build to test it ^^;

Posting Permissions

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