How to store uploaded file to local file system using xPages upload control?

StackOverflow https://stackoverflow.com/questions/9482085

  •  13-11-2019
  •  | 
  •  

Question

How to store uploaded file to local disk using xPages upload control? I have just a simple form with text field and fileUpload control on my xPages.(there are no binding to document so I'm accessing xpages components to get their values on submit) How can I access such uploaded file from my java code? I can get this upload control from my java code so I have 'XspFileUpload' object. But I cant see any way how to access the raw File object to be able to save it on files system ... Can someone help me with this?

Was it helpful?

Solution

To retrieve a file from an upload control you can use this piece of code ( its java so you need to convert it to ssjs..)

// get file from httpservletrequest 

HttpServletRequest hsr = (HttpServletRequest) FacesContext      .getCurrentInstance().getExternalContext().getRequest();
fileUploadID = 'XspFileUpload control'.getClientId(FacesContext.getCurrentInstance());
Map<?, ?> map = hsr.getParameterMap();
UploadedFile f = ((UploadedFile) map.get(fileUploadID));

if (f == null) {
  throw new java.lang.Exception("File could not be found");
}

String fileName = f.getServerFileName()
if (super.isValid() && !this.isHidden()) {
   File serverFile = f.getServerFile();
   if (serverFile != null && serverFile.exists()) {
       String dir = serverFile.getParent();
       File tempFile = new File(dir + File.separator + fileName); // create a handle to the file on server 
   }
}

OTHER TIPS

This is the SSJS code written using the answer from jjtbsomhorst, and the code from http://xpageswiki.com/web/youatnotes/wiki-xpages.nsf/dx/Work_with_a_file_upload_and_download_controls

var con = facesContext.getExternalContext(); 
var request:com.sun.faces.context.MyHttpServletRequestWrapper = con.getRequest(); 
var map:java.util.Map = request.getParameterMap(); 
var fileDataName = "view:_id1:file"; 
var fileData:com.ibm.xsp.http.UploadedFile = map.get( fileDataName ); 

if (fileData == null) {
    getComponent("message").value = "File could not be found on " + fileDataName;
}

var fileName = fileData.getServerFileName();
var serverFile:java.io.File = fileData.getServerFile();
if (serverFile != null && serverFile.exists()) {
    var dir = serverFile.getParent();
    var tempFile:java.io.File = new java.io.File(fileName);
    var correctedFileName = dir + java.io.File.separator + fileData.getClientFileName();
    var correctedFile:java.io.File = new java.io.File( correctedFileName ); 
    var success = tempFile.renameTo(correctedFile);
    getComponent("message").value = "Yay!" + correctedFileName;
    //correctedFile.renameTo(tempFile);
}
else {
    getComponent("message").value = "There's a problem to find the temporal file.";
}

PS. There's a label named "message" in the XPage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top