Uploading multiple files

23 views
Skip to first unread message

Lakhi Calantoc

unread,
Feb 20, 2019, 8:30:33 AM2/20/19
to ninja-framework
Hello Ninja Team, Do you have other examples of upload functionality when uploading multiple files? using the DiskItemProvider class? Thanks!

jonatha...@gmail.com

unread,
Feb 21, 2019, 3:31:40 AM2/21/19
to ninja-framework

Hi.


Here is a really quick example of how it can work. It assumes your input field for multiple files is called "upload".


    public Result upload(Context context) {

        try {

    for (FileItem fileItem : context.getParameterAsFileItems("upload")) {

                String fileName = FilenameUtils.normalize(fileName);

                File file = buildPath(fileName);

                if (file.exists()) {

                    throw new BadRequestException("File already exist");

                }

                FileUtils.forceMkdirParent(file);

                FileUtils.copyInputStreamToFile(stream, file);

                // For logging purpose : FileUtils.byteCountToDisplaySize(file.length());

    }

        } catch (IOException e) {

            throw new BadRequestException(e.getMessage(), e);

        }

        return Results.ok();

    }


    public String buildPath(String fileName) {

// TODO return the path where the file should be really stored

return null;

    }



FilenameUtils and FileUtils are Apache commons-io classes (by default available in ninja's dependencies I think).
I will work with disk and memory file provider. You "only" need to provide the final path where to store the file.

I hope it helps you,

Jonathan

Lakhi Calantoc

unread,
Mar 4, 2019, 7:38:33 PM3/4/19
to ninja-framework
Thanks very much!
Reply all
Reply to author
Forward
0 new messages