Cordova 如何使用Phonegap FileTransfer.upload将文件从手机摄像头上载到xPages?

Cordova 如何使用Phonegap FileTransfer.upload将文件从手机摄像头上载到xPages?,cordova,xpages,Cordova,Xpages,有人在使用IBMDominoXPages时得到了这个吗?我在这里读过很多像这样的资料 。。。但仍然无法让它工作。当我把它上传到公共的LN url,例如/my_app.nsf/upload?CreateDocument时,我得到的图像文件是richtext字段中的文本,甚至被截断。但我无法获得文件时,上传到xPages应用程序使用文件上传控制。它与此控件不关联。成功完成此操作的任何人?FileTransfer API的upload()方法将执行多部分发布到目标位置。您应该能够使用XPage处理该

有人在使用IBMDominoXPages时得到了这个吗?我在这里读过很多像这样的资料

。。。但仍然无法让它工作。当我把它上传到公共的LN url,例如/my_app.nsf/upload?CreateDocument时,我得到的图像文件是richtext字段中的文本,甚至被截断。但我无法获得文件时,上传到xPages应用程序使用文件上传控制。它与此控件不关联。成功完成此操作的任何人?

FileTransfer API的upload()方法将执行多部分发布到目标位置。您应该能够使用XPage处理该问题。我还没有用PhoneGap完成这项工作,但已经用其他各种文件上传器完成了。它需要两件事:一个XPage是上传文件的接收目标,另一个Java类处理它

XPage很简单,可以如下所示:

<?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>
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"; // name of the multipart
                                                    // POST param that holds
                                                    // the uploaded file
private static String RT_ITEM_NAME_FILES = "file"; // name of the RT item
                                                    // that will hold the
                                                    // uploaded 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();

        //only HTTP POST is allowed
        HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw (new Exception("only POST is allowed"));
        }

        Database dbCurrent = (Database) resolveVariable("database");

        //set up output object
        response.setContentType("text/plain");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        //check if we have a file in the POST
        Map map = request.getParameterMap();

        if (!map.containsKey(FILE_PARAM)) {
            throw (new Exception("no file received"));
        }

        //get the file from the request
        uploadedFile = (UploadedFile) map.get(FILE_PARAM);

        if (uploadedFile == null) {
            throw (new Exception("that's not a file!"));
        }

        //store file in a document
        fileName = uploadedFile.getClientFileName();   //original name of the file

        File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name

        //we rename the file to its original name, so we can attach it with that name
        //see http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=nl
        correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName);
        boolean renamed = tempFile.renameTo(correctedFile);

        if (renamed) {

            //create a document in the current db
            doc = dbCurrent.createDocument();
            doc.replaceItemValue("form", "fFile");

            //attach file to target document
            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) {
                // rename temporary file back to its original name so it's
                // automatically
                // deleted by the XPages engine
                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);
}

}
FileTransfer API的upload()方法执行到目标位置的多部分POST。您应该能够使用XPage处理该问题。我还没有用PhoneGap完成这项工作,但已经用其他各种文件上传器完成了。它需要两件事:一个XPage是上传文件的接收目标,另一个Java类处理它

XPage很简单,可以如下所示:

<?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>
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"; // name of the multipart
                                                    // POST param that holds
                                                    // the uploaded file
private static String RT_ITEM_NAME_FILES = "file"; // name of the RT item
                                                    // that will hold the
                                                    // uploaded 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();

        //only HTTP POST is allowed
        HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw (new Exception("only POST is allowed"));
        }

        Database dbCurrent = (Database) resolveVariable("database");

        //set up output object
        response.setContentType("text/plain");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        //check if we have a file in the POST
        Map map = request.getParameterMap();

        if (!map.containsKey(FILE_PARAM)) {
            throw (new Exception("no file received"));
        }

        //get the file from the request
        uploadedFile = (UploadedFile) map.get(FILE_PARAM);

        if (uploadedFile == null) {
            throw (new Exception("that's not a file!"));
        }

        //store file in a document
        fileName = uploadedFile.getClientFileName();   //original name of the file

        File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name

        //we rename the file to its original name, so we can attach it with that name
        //see http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=nl
        correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName);
        boolean renamed = tempFile.renameTo(correctedFile);

        if (renamed) {

            //create a document in the current db
            doc = dbCurrent.createDocument();
            doc.replaceItemValue("form", "fFile");

            //attach file to target document
            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) {
                // rename temporary file back to its original name so it's
                // automatically
                // deleted by the XPages engine
                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);
}

}
FileTransfer API的upload()方法执行到目标位置的多部分POST。您应该能够使用XPage处理该问题。我还没有用PhoneGap完成这项工作,但已经用其他各种文件上传器完成了。它需要两件事:一个XPage是上传文件的接收目标,另一个Java类处理它

XPage很简单,可以如下所示:

<?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>
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"; // name of the multipart
                                                    // POST param that holds
                                                    // the uploaded file
private static String RT_ITEM_NAME_FILES = "file"; // name of the RT item
                                                    // that will hold the
                                                    // uploaded 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();

        //only HTTP POST is allowed
        HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw (new Exception("only POST is allowed"));
        }

        Database dbCurrent = (Database) resolveVariable("database");

        //set up output object
        response.setContentType("text/plain");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        //check if we have a file in the POST
        Map map = request.getParameterMap();

        if (!map.containsKey(FILE_PARAM)) {
            throw (new Exception("no file received"));
        }

        //get the file from the request
        uploadedFile = (UploadedFile) map.get(FILE_PARAM);

        if (uploadedFile == null) {
            throw (new Exception("that's not a file!"));
        }

        //store file in a document
        fileName = uploadedFile.getClientFileName();   //original name of the file

        File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name

        //we rename the file to its original name, so we can attach it with that name
        //see http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=nl
        correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName);
        boolean renamed = tempFile.renameTo(correctedFile);

        if (renamed) {

            //create a document in the current db
            doc = dbCurrent.createDocument();
            doc.replaceItemValue("form", "fFile");

            //attach file to target document
            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) {
                // rename temporary file back to its original name so it's
                // automatically
                // deleted by the XPages engine
                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);
}

}
FileTransfer API的upload()方法执行到目标位置的多部分POST。您应该能够使用XPage处理该问题。我还没有用PhoneGap完成这项工作,但已经用其他各种文件上传器完成了。它需要两件事:一个XPage是上传文件的接收目标,另一个Java类处理它

XPage很简单,可以如下所示:

<?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>
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"; // name of the multipart
                                                    // POST param that holds
                                                    // the uploaded file
private static String RT_ITEM_NAME_FILES = "file"; // name of the RT item
                                                    // that will hold the
                                                    // uploaded 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();

        //only HTTP POST is allowed
        HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw (new Exception("only POST is allowed"));
        }

        Database dbCurrent = (Database) resolveVariable("database");

        //set up output object
        response.setContentType("text/plain");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        //check if we have a file in the POST
        Map map = request.getParameterMap();

        if (!map.containsKey(FILE_PARAM)) {
            throw (new Exception("no file received"));
        }

        //get the file from the request
        uploadedFile = (UploadedFile) map.get(FILE_PARAM);

        if (uploadedFile == null) {
            throw (new Exception("that's not a file!"));
        }

        //store file in a document
        fileName = uploadedFile.getClientFileName();   //original name of the file

        File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name

        //we rename the file to its original name, so we can attach it with that name
        //see http://www.bleedyellow.com/blogs/m.leusink/entry/processing_files_uploaded_to_an_xpage?lang=nl
        correctedFile = new java.io.File(tempFile.getParentFile().getAbsolutePath() + java.io.File.separator + fileName);
        boolean renamed = tempFile.renameTo(correctedFile);

        if (renamed) {

            //create a document in the current db
            doc = dbCurrent.createDocument();
            doc.replaceItemValue("form", "fFile");

            //attach file to target document
            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) {
                // rename temporary file back to its original name so it's
                // automatically
                // deleted by the XPages engine
                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);
}

}

修复了我的代码示例中的错误。感谢您指出这一点(顺便说一句:看到您也提出了这一更改,但出于某些原因,一些评论员错误地拒绝了)。您好@MarkLeusink-我们如何将这个Java类添加到xPage?如果我进入代码>Java>新Java类,我必须添加包、名称、超类等。。。这对许多人来说可能是显而易见的-不知道如何添加它,以便我可以看到它是如何工作的?修复了我代码示例中的错误。感谢您指出这一点(顺便说一句:看到您也提出了这一更改,但出于某些原因,一些评论员错误地拒绝了)。您好@MarkLeusink-我们如何将这个Java类添加到xPage?如果我进入代码>Java>新Java类,我必须添加包、名称、超类等。。。这对许多人来说可能是显而易见的-不知道如何添加它,以便我可以看到它是如何工作的?修复了我代码示例中的错误。感谢您指出这一点(顺便说一句:看到您也提出了这一更改,但出于某些原因,一些评论员错误地拒绝了)。您好@MarkLeusink-我们如何将这个Java类添加到xPage?如果我进入代码>Java>新Java类,我必须添加包、名称、超类等。。。这对许多人来说可能是显而易见的-不知道如何添加它,以便我可以看到它是如何工作的?修复了我代码示例中的错误。感谢您指出这一点(顺便说一句:看到您也提出了这一更改,但出于某些原因,一些评论员错误地拒绝了)。您好@MarkLeusink-我们如何将这个Java类添加到xPage?如果我进入代码>Java>新Java类,我必须添加包、名称、超类等。。。这对许多人来说可能是显而易见的-不知道如何添加它,以便我可以看到它是如何工作的?