David Leedy
David Leedy

Reputation: 3593

How do you get the filename from the XPages FileUpload Control

In XPages, in the file upload control, after a user selects a file but before it's saved how can you get the filename? I'm not interested in the path as I believe that's not getable due to security issues but I would like to get the filename and extension if at all possible.

Thanks!

Upvotes: 1

Views: 3518

Answers (2)

Toby Samples
Toby Samples

Reputation: 2178

It sounds like you are referring to needing to get the data via CSJS, which you can do with the following code:

var filename = dojo.byId('#{id:fileUpload1}').value.split('\\').pop();

Upvotes: 1

Jeremy Hodge
Jeremy Hodge

Reputation: 1665

Actually you can get the file and fully manipulate it, read it, do whatever you want with it, its stored in the xsp folder on the server, to which you have read/write access... here is a code snippet that interacts with the file, I usually call from beforeRenderResponse...

var fileData:com.ibm.xsp.http.UploadedFile = facesContext.getExternalContext().getRequest().getParameterMap().get(getClientId('<INSERT ID OF UPLOAD CONTROL HERE (ie. fileUpload1)>'));

if (fileData != null) {
    var tempFile:java.io.File = fileData.getServerFile();
    // Get the path
    var filePath:String = tempFile.getParentFile().getAbsolutePath();
    // Get file Name
    var fileName:String = tempFile.getParentFile().getName();
    // Get the Name of the file as it appeared on the client machine - the name on the server will NOT be the same
    var clientFileName:String = fileData.getClientFileName();
}

Upvotes: 6

Related Questions