The upload() method of the FileTransfer API performs a multi-part POST to a target location. You should be able to process that with an XPage. I haven't done it with PhoneGap (yet), but have with various other file uploaders. It requires two things: an XPage that is the receiving target for the uploaded file and a Java class to process it.
The XPage is simple and can look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.beforePageLoad>
<![CDATA[#{javascript:eu.linqed.UploadHandler.process();}]]>
</xp:this.beforePageLoad>
</xp:view>
The rendered property is set to false so it doesn't output anything (we can handle that in our Java class). We also don't need a file upload control: the uploaded file is read directly from the request.
The Java class could look like this:
package eu.linqed;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.*;
import com.ibm.xsp.http.UploadedFile;
import com.ibm.xsp.webapp.XspHttpServletResponse;
public class UploadHandler {
private static String FILE_PARAM = "uploadedFile";
private static String RT_ITEM_NAME_FILES = "file";
public UploadHandler() {
}
@SuppressWarnings("unchecked")
public static void process() {
XspHttpServletResponse response = null;
PrintWriter pw = null;
UploadedFile uploadedFile = null;
File correctedFile = null;
RichTextItem rtFiles = null;
Document doc = null;
String fileName = "";
FacesContext facesContext = FacesContext.getCurrentInstance();
try {
ExternalContext extCon = facesContext.getExternalContext();
response = (XspHttpServletResponse) extCon.getResponse();
pw = response.getWriter();
HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
if (!request.getMethod().equalsIgnoreCase("post")) {
throw (new Exception("only POST is allowed"));
}
Database dbCurrent = (Database) resolveVariable("database");
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
Map map = request.getParameterMap();
if (!map.containsKey(FILE_PARAM)) {
throw (new Exception("no file received"));
}
uploadedFile = (UploadedFile) map.get(FILE_PARAM);
if (uploadedFile == null) {
throw (new Exception("that's not a file!"));
}
fileName = uploadedFile.getClientFileName();
File tempFile = uploadedFile.getServerFile();
correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName);
boolean renamed = tempFile.renameTo(correctedFile);
if (renamed) {
doc = dbCurrent.createDocument();
doc.replaceItemValue("form", "fFile");
rtFiles = doc.createRichTextItem(RT_ITEM_NAME_FILES);
rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null);
boolean saved = doc.save();
}
pw.print("add code to return to the upload method here");
response.commitResponse();
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
pw.print("add code here to return an error");
try {
response.commitResponse();
} catch (IOException e1) {
e1.printStackTrace();
}
} finally {
facesContext.responseComplete();
try {
if (rtFiles != null) {
rtFiles.recycle();
}
if (doc != null) {
doc.recycle();
}
if (correctedFile != null) {
correctedFile.renameTo(uploadedFile.getServerFile());
}
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
private static Object resolveVariable(String variableName) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, variableName);
}
}