Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

how to upload file (xml, doc, pdf etc)

Yi Chen
2012-02-29
2013-04-08
  • Yi Chen
    Yi Chen
    2012-02-29

    Hi all,
    I want to upload the file into the database. I have read the wiki Uploading and processing a file from client (multipart form) in Chapter 7: Controllers. But this is too simple.

    I want to do is uploading file, changing file, deleting file and displaying file (this is optional).
    I think to achieve this goal, it must relates with Stereotype, action, editor jsp and controller xml. But the wiki just mentioned parts of these.

    I hope someone can give me a clear explanation step by step. Thank you.

     
  • Javier Paniza
    Javier Paniza
    2012-03-02

    Hi Yi,

    from client (multipart form) in Chapter 7: Controllers. But this is too simple.

    Of course, it explains the way a file can be upload from an OX applications, the things you want to do with it is your business.

    But the wiki just mentioned parts of these

    The wiki cannot contain any possible case you can face when developing an application, because there are infinite cases.

    I want to do is uploading file, changing file, deleting file and displaying file

    There is no problem in doing any of these things, you have all the power of Java in your hands. What's the concrete problem?


    Do you want a better answer?Try the OpenXava professional support

     
  • Can I make file uploading without showing any modal windows ?

     
  • Laurent
    Laurent
    2013-03-28

    Of course, you can do without a modal dialog:

    In your controller:

    <controller name="UploadImport">
        <action name="chooseFile" hidden="true" class="com.subrenat.extranet.actions.FindFileToUploadAction" >
            <set property="controller" value="UploadImport" />
        </action>
        <action name="uploadFile" class="com.subrenat.extranet.actions.UploadImportFileAction" />
        <action name="return" mode="detail" image="images/back.gif" class="org.openxava.actions.ReturnAction"/>             
    </controller>
    

    In your actions:

    public class FindFileToUploadAction extends BaseAction implements ILoadFileAction {
    
        private String controller;
    
        public void execute() throws Exception { 
        }
    
        public String getNextMode() {
            return DETAIL;
        }
    
        public String[] getNextControllers() {
            return new String [] { getController() };
        }
    
        public String getCustomView() {
            return "xava/editors/uploadFile";
        }
    
        public boolean isLoadFile() { 
            return true;
        }
    
        public void setController(String controller) {
            this.controller = controller;
        }
    
        public String getController() {
            return controller;
        }
    }
    

    And the other action class

    public class UploadImportFileAction extends ViewBaseAction implements INavigationAction, IProcessLoadedFileAction {
    
        @SuppressWarnings("unchecked")
        private List fileItems;
    
        private static Log log = LogFactory.getLog(UploadImportFileAction.class);
    
        @SuppressWarnings("unchecked")
        public void execute() throws Exception {
            Iterator i = getFileItems().iterator();
            while (i.hasNext()) {
                FileItem fi = (FileItem)i.next();  
                String fileName = fi.getName();
                if (!Is.emptyString(fileName)) {
                    if (fileName.indexOf('/') != -1) fileName = fileName.substring(fileName.indexOf('/')+1);
                    if (fileName.indexOf('\\') != -1) fileName = fileName.substring(fileName.indexOf('\\')+1);
                    getView().setValue("fileName", fileName);
                    File f = new File(Parameter.get(ImporterDaemon.IMPORT_PATH) + fileName);
                    log.info("Uploading " + f.getAbsolutePath());
                    fi.write(f);
                }
            }
        }
    
        public String[] getNextControllers() {
            return PREVIOUS_CONTROLLERS;
        }
    
        public String getCustomView() {     
            return PREVIOUS_VIEW; 
        }
    
        @SuppressWarnings("unchecked")
        public List getFileItems() {
            return fileItems;
        }
    
        @SuppressWarnings("unchecked")
        public void setFileItems(List fileItems) {
            this.fileItems = fileItems;
        }
    }
    
     
    Last edit: Laurent 2013-03-28
  • Thank you for request, Laurent! It works, but in this case user has to go to another form for upload file. Is it possible to upload file directly in viewDetail action resulting page ?

     
    Last edit: Sergey Melnikov 2013-03-29
  • Javier Paniza
    Javier Paniza
    2013-04-02

    Hi Sergey,

    Is it possible to upload file directly in viewDetail action resulting page ?

    No, because it need to perform a multpart form request to upload the file, and the form for regular view are not multipart, even more, they are not submited but the data is sent to the server via AJAX.

    An option would be create a new editor to do the upload via AJAX or using HTML5. Are you willing to develop this editor and contribute it to OpenXava?


    Help others in this forum as I help you.
    Need more help? Get OpenXava professional support.

     
  • Hello, Javier!

    Are you willing to develop this editor and contribute it to OpenXava?

    No, we didn't find any solution and decided to make uploading file in another form . Unfortunately, upload via AJAX is very complicated for me..