Page 1 of 4 123 ... LastLast
Results 1 to 10 of 35

Thread: How to create a file upload browser in spring roo?

  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Red face How to create a file upload browser in spring roo?

    I'm new in spring roo. I want to create a page with file upload. I used spring roo to create all pages and i try to use it to create a file browser button in file upload page. The problem is spring roo using spring form tag which doesn't have file browser. I solve this problem by using html input type="file" tag instead, but the spring roo showed the error like this "Failed to invoke handler method [public void egat.spring.roo.ptu.io.web.ImportExcelController.p ost(java.lang.Long,org.springframework.ui.ModelMap ,javax.servlet.http.HttpServletRequest,javax.servl et.http.HttpServletResponse)]; nested exception is java.lang.IllegalStateException: Could not find @PathVariable [id] in @RequestMapping "

    How can i solve this problem?

  2. #2

    Default

    Hi,

    I also struggled with fileupload maybe my post os stackoverlfow can help you.

    HTH

  3. #3
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    You may like to vote for or watch issue https://jira.springsource.org/browse/ROO-442.
    Ben Alex
    Project Founder, Spring UAA, Spring Roo and Spring Security

  4. #4
    Join Date
    Feb 2010
    Posts
    7

    Default a way for upload file

    tested in Roo 1.0.2

    1: modify create.jsp: add enctype="multipart/form-data" and <input type="file" name="file"/>

    PHP Code:
    <form:form action="${form_url}method="POST" modelAttribute="big" enctype="multipart/form-data">
      
    file:  <input type="file" name="file" /> 

    2: modify entity:

    PHP Code:

    @Entity
    @RooJavaBean
    @RooToString
    @RooEntity
    public class Big {

        @
    Size(max 10240)
        private 
    String content;

        @
    Size(max 20)
        private 
    String name;

        @
    Transient
        
    private CommonsMultipartFile file// added

        
    private String fileName// added
        
    private long size//added 

        
    public CommonsMultipartFile getFile() {
            return 
    file;
        }

           
    // save file to disk ,save filename , file size to database 
        
    public void setFile(CommonsMultipartFile file) {
            
    this.file file;
            
    this.fileName file.getOriginalFilename();
            
    this.size file.getSize();
            
    System.out.println(" hehe  this.fileName: " this.fileName " ,  "
                    
    file.getClass().getName());

            try {
                
    InputStream in file.getInputStream();
                
    FileOutputStream f = new FileOutputStream("f:/work/tempDir/"
                        
    + new Date().getTime());

                
    int ch 0;
                while ((
    ch in.read()) != -1) {
                    
    f.write(ch);
                }
                
    f.flush();
                
    f.close();
            } catch (
    FileNotFoundException e) {
                
    // TODO Auto-generated catch block
                
    e.printStackTrace();
            } catch (
    IOException e) {
                
    // TODO Auto-generated catch block
                
    e.printStackTrace();
            }

        } 

  5. #5

    Default

    Alternatively you can also store the file content directly into the database by using a propertyeditor to bind the file-contents to a byte-array property of your entity.

    Depending on your usecase this may or may not be a good idea. It is a tradeoff between database size and speed of file retrieval (db-io is probably faster than filesystem-io)

    See this thread for a full discussion.

  6. #6
    Join Date
    May 2010
    Posts
    8

    Default

    thankz huang, your code working perfectly..

  7. #7
    Join Date
    Jul 2010
    Posts
    1

    Default trying with 1.1.0 M2

    I'm trying this solution with Roo 1.1.0 M2 and not having much luck. I've munged around with the generated jspx file and it appears to be generating the proper HTML (multipart encoded, <input type="file">), but my object never has setFileData called on it.

    Has anyone gotten file upload to work simply with Roo 1.1.0 M2?

    -pete

  8. #8
    Join Date
    Jul 2010
    Posts
    1

    Default thanks

    Nice, Huang! Thank you for helping out... I was also having same issue, your instructions worked out with mine too!

  9. #9
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default

    Quote Originally Posted by huang View Post
    tested in Roo 1.0.2 (...)
    Your approach combined with the modified tags as described in https://jira.springsource.org/browse/ROO-442 (I actually added a field:file tag) worked for me as well (Roo 1.1.0) including storage in the database. I had to add commons-io as dependency to get it to work.

  10. #10
    Join Date
    Oct 2010
    Location
    Almere, The Netherlands
    Posts
    32

    Default Where are the Dojo CSS files?

    I am trying to get the file upload styled nicely.

    In the Dojo demo (http://demos.dojotoolkit.org/demos/u.../?forceNoFlash) they have styles like 'dijitFileInputReal' for the file upload but I don't see them in the included ROO Dojo CSS files, which I can't find anywhere.

    If you leave the default ROO form styling with Spring.addDecoration the file upload field is not usable (at least on a Mac).

Tags for this Thread

Posting Permissions

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