How to save downloaded file to local file system using xPages download control?

How to save downloaded file to local disk using xPages download control? I have a simple form with a text box and file control on my xPages. (There is no binding to the document, so I get access to the xpages components to get their values ​​when sending). How can I access such a downloaded file from my java code? I can get this load control from my java code, so I have an XspFileUpload object. But I can’t figure out how to access the raw file object in order to save it to the file system ... Can someone help me with this?

+2
source share
2 answers

To extract a file from a download control, you can use this piece of code (its java 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 } } 
+2
source

This is the SSJS code written using a response 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 a problem to find the temporal file."; } 

PS. XPage has a label called "message."

0
source

All Articles