cancel
Showing results for 
Search instead for 
Did you mean: 

ReferenceError: 'formdata' is not defined

esideveloper
Champ in-the-making
Champ in-the-making
Hello,

I have prototyped a file upload form, similar to the example posted in Alfresco wiki (http://wiki.alfresco.com/wiki/Web_Scripts_Examples#File_Upload).

I keep getting this error everytime: ReferenceError: "formdata" is not defined.

Not sure why that is the case. Any ideas??
4 REPLIES 4

sujaypillai
Confirmed Champ
Confirmed Champ
Make sure your form tag has - enctype="multipart/form-data" as shown below -

<form action="${url.service}" method="post" enctype="multipart/form-data" accept-charset="utf-8">

esideveloper
Champ in-the-making
Champ in-the-making
Thanks for your reply. I have done so, as you said, but the problem still exists. The only difference between my example and the one in the link above is that both the form page and the page to process the file upload have different name, and both are a post, i.e.:

- ProfilePhotoUpload.post.html.ftl: presents that form to the user

- ProfilePhotoUploadDone.post.html.ftl: suppose to upload photo to Alfresco

sujaypillai
Confirmed Champ
Confirmed Champ
ProfilePhotoUpload.post.html.ftl: presents that form to the user
Does that really work for you?

Both verbs "GET" & "POST" explain their significance itself.

When you need to present a form to the user, you should GET it from the server and when the user fills in the data he/she will POST it.

Here are below the files (with different names) that should work for you too:
profilephotoupload.get.desc.xml
<webscript>
  <shortname>Profile Photo Upload Form Sample</shortname>
  <description>Form for uploading Photo content and meta-data into Repository</description>
  <url>/sample/profileupload</url>
  <authentication>user</authentication>
</webscript>

profilephotoupload.get.html.ftl

<html>
<head>
   <title>Upload Web Script Sample</title>
   <link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css">
</head>
<body>
   <table>
     <tr>
       <td><img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
       <td><nobr>Upload Web Script Sample</nobr></td>
     </tr>
     <tr><td><td>Alfresco ${server.edition} v${server.version}
   </table>
   <p>
   <table>
     <form action="${url.service}" method="post" enctype="multipart/form-data" accept-charset="utf-8">
       <tr><td>File:</td><td><input type="file" name="file"></td></tr>
       <tr><td>Title:</td><td><input name="title"></td></tr>
       <tr><td>Description:</td><td><input name="desc"></td></tr>
       <tr><td></td></tr>
       <tr><td><input type="submit" name="submit" value="Upload"></td></tr>
     </form>
   </table>
</body>
</html>

profilephotouploaddone.post.desc.xml

<webscript>
  <shortname>File Upload Sample</shortname>
  <description>Upload file content and meta-data into Repository</description>
  <url>/sample/profileupload</url>
  <authentication>user</authentication>
</webscript>

profilephotouploaddone.post.html.ftl

<html>
<head>
   <title>Upload Web Script Sample</title>
   <link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css">
</head>
<body>
   <table>
     <tr>
       <td><img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" /></td>
       <td><nobr>Upload Web Script Sample</nobr></td>
     </tr>
     <tr><td>Alfresco ${server.edition} v${server.version}</td></tr>
     <tr><td></td></tr>
     <tr><td>Uploaded <a href="${url.serviceContext}/sample/folder${upload.displayPath}">${upload.name}</a> of size ${upload.properties.content.size}.</td></tr>
   </table>
</body>
</html>

profilephotouploaddone.post.js

var filename = null;
var content = null;
var title = "";
var description = "";

// locate file attributes
for each (field in formdata.fields)
{
  if (field.name == "title")
  {
    title = field.value;
  }
  else if (field.name == "desc")
  {
    description = field.value;
  }
  else if (field.name == "file" && field.isFile)
  {
    filename = field.filename;
    content = field.content;
  }
}

// ensure mandatory file attributes have been located
if (filename == undefined || content == undefined)
{
  status.code = 400;
  status.message = "Uploaded file cannot be located in request";
  status.redirect = true;
}
else
{
  // create document in company home for uploaded file
  upload = companyhome.createFile("upload" + companyhome.children.length + "_" + filename) ;
 
  upload.properties.content.write(content);
  upload.properties.content.setEncoding("UTF-8");
  upload.properties.content.guessMimetype(filename);
 
  upload.properties.title = title;
  upload.properties.description = description;
  upload.save();

  // setup model for response template
  model.upload = upload;
}


The other point to be noted is that both the webscript have same <url> in description and for the same reason your form uses -
<form action="${url.service}" method="post" enctype="multipart/form-data" accept-charset="utf-8">

Say that you had a <url> set for second webscript [profilephotouploaddone] as - /sample/profileuploaddone then your form tag should be -
<form action="${url.serviceContext}/sample/profileuploaddone" method="post" enctype="multipart/form-data" accept-charset="utf-8">


Hope so that helps you Smiley Happy

Thanks.

esideveloper
Champ in-the-making
Champ in-the-making
Thanks for your reply. What you explained made sense. To shed some more light into my situation is that we have a middle-man (Web Quick Start) acting as a proxy between the end-user and Alfresco.

To reiterate, the following works for file upload (through quick start):

- ProfilePhotoUpload.get.html.ftl for presenting the form
- ProfilePhotoUpload.post.js for uploading the file

Both have the same URI. But now, we set up Alfresco to only process POST requests (won't handle GET requests because the GET's we execute are too large today for a GET request). That is why we had to resort to the implementation above, which is proving to be problematic. So:

- ProfilePhotoUpload.post.html.ftl DOES present the form to end-user
- ProfilePhotoUploadDone.post.js throws the error ReferenceError: "formdata" is not defined

Thanks again for all your input, cheers