cancel
Showing results for 
Search instead for 
Did you mean: 

UploadContentServlet works, what do I do now?

ejholmgren
Champ in-the-making
Champ in-the-making
I currently have the following ColdFusion code working, and the upload is successful returning an HTTP 200 OK response:

<cfhttp url="#APPLICATION.alfrescoRoot#/service/api/login?u=#APPLICATION.alfrescoUsername#&pw=#APPLICATION.alfrescoPassword#" method="GET"/>

<cfset myTicket = XMLParse(CFHTTP.FileContent).ticket.XmlText>

<cfhttp url="http://localhost:8080/alfresco/upload/test.pdf?ticket=#myTicket#" method="put">
   <cfhttpparam type="file" name="myFile" file="C:\test.pdf">
</cfhttp>

<cfdump var="#CFHTTP#">

The content of the response contains contentUrl=store://2009/2/10/11/51/81a3a5f2-f79b-11dd-82dd-77dded3c2072.bin|mimetype=application/pdf|size=57601|encoding=UTF-8|locale=en_US_ which I assume is the nodeRef, mimetype, size, encoding, etc … but how can I move it somewhere in the repository so it is accessible (ie. to /Company Home/Upload/)?  Logically, I need to write a webscript that either moves this node somewhere else in the repository or creates a node beforehand to stream the data into. I've written a few webscripts already, but can't figure out how to even start on this one. Any help would be greatly appreciated.

Eric
3 REPLIES 3

ejholmgren
Champ in-the-making
Champ in-the-making
Got it working with a ColdFusion demo only to implement the upload in Flex and find out that the current flash player doesn't support HTTP PUT.  :shock:  Looks like I'm going to have to POST against a single webscript instead. Hope this code sample helps someone out though. Feel free to ask any questions.

createNode.get.desc.xml:

<webscript>
    <shortname>createNode</shortname>
    <description>Create a node to stream file upload from UploadContentServlet into</description>
    <url>/createNode?fileName={fileNameArgument}</url>    
    <format default="xml">argument</format>
    <authentication>user</authentication>
</webscript>

createNode.get.js:

var name = args.fileName;

var resultString = "Action failed";
var resultCode = false;

try {
    resultString = "Could not create node";
    var nodeNew;
    var nodeRef;

    if((name == null) || (name == ""))
    {
        resultString = "node must be given a name";
    }
    else
    {
        nodeParent = roothome.childByNamePath("Company Home/Upload");
        var nodeNew = nodeParent.createNode(name, "cm:content");
    }
    if(nodeNew != null)
    {
        resultString = "nodeRef created";
        resultCode = true;
        nodeRef = nodeNew.nodeRef;
    }
}
catch (e)
{
    resultString = "Action failed due to exception";
}

model.resultString = resultString;
model.resultCode = resultCode;
model.nodeRef = nodeRef;

createNode.get.xml.ftl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<result>
    <resultString>${resultString}</resultString>
    <resultCode>${resultCode?string}</resultCode>
    <nodeRef>${nodeRef?string}</nodeRef>
</result>

ColdFusion code:

<!— Get a connection ticket —>
<cfset httpURL = "http://" & alfrescoRoot & "/service/api/login?u=" & alfrescoUsername & "&pw=" & alfrescoPassword>
<cfhttp url="#httpURL#" method="GET" />
<cfset ticket = XMLParse(Trim(CFHTTP.FileContent)).ticket.XmlText>

<!— Create a node in the repository with the createNode webscript —>
<cfset httpURL = "http://" & alfrescoRoot & "/service/createNode?fileName=test.pdf&alf_ticket=" & ticket>
<cfhttp url="#httpURL#" method="GET" />
<cfset nodeRef = XMLParse(CFHTTP.FileContent).result.nodeRef.xmlText>
<cfset nodeRef = Right(nodeRef, Len(nodeRef) - (Find("workspace://SpacesStore/", nodeRef) + 23))>

<!— Send the file to the UploadContentServlet with an HTTP PUT —>
<cfset httpURL = "http://" & alfrescoRoot & "/upload/workspace/SpacesStore/" & nodeRef & "/test.pdf?ticket=" & ticket">
<cfhttp url="#httpURL#" method="PUT">
   <cfhttpparam type="file" name="testFile" file="C:\test.pdf">
</cfhttp>

mikeh
Star Contributor
Star Contributor
We coded in HTTP method tunnelling to work around those Flash/Flex limitations.

Have a look here for details: http://wiki.alfresco.com/wiki/Web_Scripts#Tunneling_HTTP_Methods

Thanks,
Mike

ejholmgren
Champ in-the-making
Champ in-the-making
Mike,

Will that work against the UploadContentServlet as well? Is there any performance/stability gain to be had from using the UploadContentServlet vs the webscript method shown at http://wiki.alfresco.com/wiki/Web_Scripts_Examples#File_Upload ?