How can I create a custom mapping that accepts MultipartFile?

LabKey Support Forum (Inactive)
How can I create a custom mapping that accepts MultipartFile? cyrus  2015-04-16 08:52
Status: Closed
 
In vanilla Spring, I'm doing this:

>>>
@RequestMapping(value="/convertXLSToJSON", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String handleFileUpload(@RequestParam("file") MultipartFile file)
{
    System.out.println("Converting XLS to json...");
    // Apache POI conversion stuff
    return somejsonobject.toString();
}
>>>

How is this accomplished in a LabKey custom module? (Specifically, I'm working in the "demo" module for now.)

Thanks in advance for any help you can provide.
 
 
kevink responded:  2015-04-16 11:08
First, we have a few JavaScript APIs that you can try using to POST a file and get back a JSON formatted response. It requires two round-trips: 1) POST the file to assay/assayFileUpload.view to create an ExpData object, (2) create a LABKEY.Exp.Data from the response and call data.getContent() to parse the Excel file into JSON.

See the example in:
https://www.labkey.org/download/clientapi_docs/javascript-api/symbols/LABKEY.Exp.Data.html

Second, to answer the question about getting a POSTed file: within your Java action call .getFileMap() -- for example:

    @RequiresPermissionClass(InsertPermission.class)
    public class ConvertXlsToJsonAction extends ApiAction
    {
        @Override
        public Object execute(Object o, BindException errors) throws Exception
        {
            Map<String, MultipartFile> files = getFileMap();

            List<String> names = new ArrayList<>();
            for (MultipartFile file : files.values())
                names.add(file.getOriginalFilename());

            JSONObject json = new JSONObject();
            json.put("fileNames", names);
            return json;
        }
    }
 
cyrus responded:  2015-04-16 11:56
Thanks kevin. Unfortunately the /convertXLSToJSON location isn't being mapped by the controller. Just to make sure, I renamed the class to "ConvertAction" and tried "/convert", but that isn't working either. I get a 404 on both.

>>>>>>>>>>>

    @RequiresPermissionClass(InsertPermission.class)
    public class ConvertAction extends ApiAction
    {
        @Override
        public Object execute(Object o, BindException errors) throws Exception
        {
            JSONObject full_jo = null;
            Map<String, MultipartFile> files = getFileMap();
            try
            {
                full_jo = new JSONObject();
                ...

>>>>>>>>>>

Sorry I'm new to LabKey and am not exactly sure how these routes are getting set up. If you have any further advice, I would appreciate it.

Cyrus
 
kevink responded:  2015-04-16 12:06
For an overview of LabKey URLs, check out this help doc:

https://www.labkey.org/wiki/home/Documentation/page.view?name=url

The routing is based on the module's registered controllers and inner classes of that controller. In the case of the DemoModule, the DemoController is registered as "demo":

    protected void init()
    {
        addController("demo", DemoController.class);
    }

Then the actions within the controller are added based on the class name minus the "Action" part. So in this case: "/labkey/demo/<container>/convertXlsToJson.view"
 
cyrus responded:  2015-04-16 14:08
That worked. I had the path wrong. And I forgot ".view". Thanks again.