cancel
Showing results for 
Search instead for 
Did you mean: 

upload.post.js unable to get fields from formdata

calebmei
Champ in-the-making
Champ in-the-making
Hi there,

I apologize if this question was asked before, but I couldn't find similar question and answer from previous posts.

I am trying to use Java to upload content via,
   
POST /service/api/upload
with the request body attached below.

However, upload.post.js is unable to get any field from formdata. When I try to print all the fields in formdata using this,
for each (field in formdata.fields) {
        status.message += " " + String(field.name);
      }

There is no fields in the formdata.

My understanding is that this is not an authentication issue, because without alf_ticket, I am getting 401 error. This issue is also unlikely to be similar /service/sample/upload, which requires permission on the user from the subspace. This issue's script is located in tomcat directory.

Is there some special setting I need to set the header of the request?
Is there  a way for me to trace the issue on why alfresco is not passing the formdata to the upload.post.js?
Anyone have similar issue?

Thanks in advance and really appreciate for any assistance.  Smiley Happy

Cheers,
Calebmei


Here's the request body with multipart form:

POST /service/api/upload
Content-Type: multipart/form-data; boundary=3673886725790367917

—3673886725790367917
Content-Disposition: form-data; name="containerid"

26a0569c-c056-4997-bbb4-67f81ec6695b
—3673886725790367917
Content-Disposition: form-data; name="nodeid"

26a0569c-c056-4997-bbb4-67f81ec6695b
—3673886725790367917
Content-Disposition: form-data; name="ticket"

TICKET_8950a48fe1117b4c32f359a06c514bd4c4f70d7c
—3673886725790367917
Content-Disposition: form-data; name="title"

test2.txt
—3673886725790367917
Content-Disposition: form-data; name="filename"

test2.txt
—3673886725790367917
Content-Disposition: form-data; name="siteid"
localhost

—3673886725790367917
Content-Disposition: form-data; name="submit"

Upload
—3673886725790367917
Content-Disposition: form-data; name="filedata"; filename="c:\test2.txt"
Content-Type: text/plain

[actual content here]
—3673886725790367917
4 REPLIES 4

damiar
Champ in-the-making
Champ in-the-making
I'm using a Java service that calls a Webscript  and send to it the arguments (including a file) without problems…
May the problem be in the service you are using to upload the file? Can you post the code?

calebmei
Champ in-the-making
Champ in-the-making
Got it working using apache lib. So definitely something wrong when I build the request using URLConnection.

http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/multipart/Multipar...
Smiley Very Happy

rpldcox
Champ in-the-making
Champ in-the-making
Alfresco is working fine for me when I use GET- and PUT-type requests (haven't ventured into DELETE yet  Smiley Surprised). POST just isn't working though.

I have a similar problem to that described in the first post in this thread. In my web script the fields in the formdata are all empty. I think I have ruled out authentication problems (the web script is executing and sending back its own error code).

This is with Alfresco Community v3.2.0 and using C# code to generate the multipart form data.

Here is a run-down of what happens:

The post request is:

POST /alfresco/service/traqsAPI/testUpload?alf_ticket=TICKET_……………………ad61da HTTP/1.1
The headers are:

Content-Type: multipart/form-data; boundary=BEACF86DE59B48bbB49F82CB55A3D009
Host: alfrescoserver:8080
Content-Length: 336
Expect: 100-continue

Alfresco replies with "100 Continue" and a blank header block.

Now the content is sent:

–BEACF86DE59B48bbB49F82CB55A3D009
Content-Disposition: form-data; name="test"

This is a test value.
–BEACF86DE59B48bbB49F82CB55A3D009
Content-Disposition: form-data; name="filedata"; filename="filename.xml"
Content-Type: text/xml

<h3>Heading</h3><p>This is some text for the test.</p>
–BEACF86DE59B48bbB49F82CB55A3D009


However, the content fields are not getting through; I know this because Alfresco replies with:

<response>
  <status>
    404
    <name>Not Found</name>
    <description>Requested resource is not available.</description>
  </status>
  <message>No content was supplied in the request. Field names found were ''</message>
  <exception></exception>
  <callstack>
  </callstack>
  <server>Alfresco Community v3.2.0 (2039) schema 2,019</server>
  <time>04-Nov-2009 22:16:30</time>
</response>

The <message> in the XML above is generated by my web script:

function main(){

   // Get values from form data.
    var filename = null;
    var content = null;
    var mimetype = null;
    var test = null;
   // Parse file attributes
   var fieldNames = "";
   for each (field in formdata.fields) {
      fieldNames = fieldNames + field.name + " ";
      switch (String(field.name).toLowerCase()) {
         case "filedata":
            if (field.isFile) {
               filename = field.filename;
               content = field.content;
               mimetype = field.mimetype;
            }
            break;

         case "test":
            test = field.value;
            break;
      }
   }
   
   if(content == null){
      status.code = 404;
      status.message = "No content was supplied in the request. Field names found were '" + fieldNames + "'";
      status.redirect = true;
      return;
   }


   …snipped…
}   

main();

I've combed the forum and the web for examples and my multipart format seems to be right, but someone here may be able to see my mistake. Or possibly you'll tell me that I just have to use java :lol:.

Thank you.

rpldcox
Champ in-the-making
Champ in-the-making
Found the problem: I missed two hyphens from the last line of my form data content.

It should have been:

–BEACF86DE59B48bbB49F82CB55A3D009–
instead of

–BEACF86DE59B48bbB49F82CB55A3D009

It is working perfectly now.

Richard.