cancel
Showing results for 
Search instead for 
Did you mean: 

Upload file from RestApi

dharmendra_jais
Champ in-the-making
Champ in-the-making
Can anyone tell the example to use
POST /alfresco/service/sample/upload (http://wiki.alfresco.com/wiki/2.1_REST_API#File_Upload_Sample)

for uploading file into alfresco repository. I am using Alfresco Lab 3

How can I use this through HttpClient.


Code I m using is :
String URL = "http://localhost:8080/alfresco/service/sample/upload";
PostMethod post = new PostMethod(URL);
NameValuePair[] data = {new NameValuePair("filename", "C:/testFile.txt"),
              new NameValuePair("mimetype", "text/plain"),            
            new NameValuePair("containerId", "7018a7e3-d366-4389-84c6-bd79d793c270")};   //Company Homes' Id      
      
post.setRequestBody(data);
int statusCode1 = client.executeMethod(post);

The error I am getting is :
  "message" : "Failed to execute script '\/org\/alfresco\/upload.post.js (in classpath store fileSmiley Very Happy:\/alfresco\/tomcat\/webapps\/alfresco\/WEB-INF\/classes\/alfresco\/webscripts)': Failed to execute script '\/org\/alfresco\/upload.post.js (in classpath store fileSmiley Very Happy:\/alfresco\/tomcat\/webapps\/alfresco\/WEB-INF\/classes\/alfresco\/webscripts)': ReferenceError: \"formdata\" is not defined. (AlfrescoScript#21)", 
"org.mozilla.javascript.EcmaError: ReferenceError: \"formdata\" is not defined. (AlfrescoScript#21)"


I will be very thankful if anyone would give me example through sample code.

Regards.
DJ
9 REPLIES 9

mikeh
Star Contributor
Star Contributor

shamabbas
Champ in-the-making
Champ in-the-making
I have been following Alfresco Developer guide book to upload file in Webscripts framework. But I'm getting same error as mentioned above"ReferenceError: "formdata" is not defined. "
Please if anyone can help? + I'm not doing these customization using Alfresco explorer. Because i don't know fully in front-end where to do these customizations. I followed Company_Home > Data_Dictionary > Web Client Extension …
I don't know how to create these files in it?
I just made these files and pasted in
"jboss-4.2.3.GA\server\default\tmp\deploy\tmp28186alfresco-exp.war\WEB-INF\classes\alfresco\webscripts" dir. Other applications i practiced worked fine but this is not working.
PLEASE HELP AS SOON AS POSSIBLE!

mikeh
Star Contributor
Star Contributor
PLEASE HELP AS SOON AS POSSIBLE!
You're going to have to supply more information, such as the code you've put together to upload a file with.

Mike

shamabbas
Champ in-the-making
Champ in-the-making
hey,
im using .js file n code in it im using is here below


for each (field in formdata.fields) {
if (field.name == "name") {
model.name = field.value;
}
if (field.name == "file" && field.isFile) {
filename = field.filename;
content = field.content;
mimetype = field.mimetype;
}
}
var results = search.luceneSearch("+PATH:\"app:company_home/*\" +TYPE:\"cm:folder\" +@cm\\:name:\"Someco\"");
var targetFolder = results[0];
var newDoc = targetFolder.createFile(filename); newDoc.properties.content.write(content); newDoc.properties.content.mimetype = mimetype;
newDoc.save();


here above formdata is being used.
it also gives somtimes error that this targetfolder is undefined. I don't know what built in functions are using because im just following Developer guide book as practise for customization. And path  "+PATH:\"app:company_home/*\" +TYPE:\"cm:folder\" +@cm\\:name:\"Someco\" i don't know about it much either If you can please help me?
Other freemarker template file codes are below here…

helloworld.get.html.ftl
<html>
<body>
<form action="${url.service}" method="post" enctype="multipart/
form-data" accept-charset="utf-8">
Name:<input name="name"><br />
File:<input type="file" name="file"><br />
<input type="submit" name="submit" value="Go!">
</form>
</body>
</html>


helloworld.post.html.ftl
<html>
<body>
<p>Hello, ${args.name}!</p>
</body>
</html>

other two files are desc.xml files  "helloworld.get.desc.xml" , "helloworld.post.desc.xml".
In post authentication is required but not in get. rest is same.
<webscript>
<shortname>Hello World Form Post</shortname>
<description>Hello world form post web script</description>
<url>/someco/helloworldform</url>
<authentication>admin</authentication>
<transaction>required</transaction>
</webscript>

shamabbas
Champ in-the-making
Champ in-the-making
and Im using Jboss server… thought to let you know.

mikeh
Star Contributor
Star Contributor
it also gives somtimes error that this targetfolder is undefined. I don't know what built in functions are using because im just following Developer guide book as practise for customization. And path  "+PATH:\"app:company_home/*\" +TYPE:\"cm:folder\" +@cm\\:name:\"Someco\" i don't know about it much either If you can please help me?
If it sometimes gets to the line where the Lucene query is running, then the formdata object must have been parsed correctly in that instance. You need to make sure you've got the "Someco" folder immediately under Company Home.

Mike

shamabbas
Champ in-the-making
Champ in-the-making
You need to make sure you've got the "Someco" folder immediately under Company Home.

Mike



Yes you are right there is no Someco folder and i don't know how to make this folder.
Yesterday I practiced  wiki example for file uploading
"http://wiki.alfresco.com/wiki/Web_Scripts_Examples#File_Upload"
It has little different code its working perfectly.
But I would like to know as asked before .. "developer guide book" gives error "formdata is undefined". This issue is not resolved.

for each (field in formdata.fields) {
if (field.name == "name") {
model.name = field.value;
}
if (field.name == "file" && field.isFile) {
filename = field.filename;
content = field.content;
mimetype = field.mimetype;
}
}
var results = search.luceneSearch("+PATH:\"app:company_home/*\" +TYPE:\"cm:folder\" +@cm\\:name:\"Someco\"");
var targetFolder = results[0];
var newDoc = targetFolder.createFile(filename); newDoc.properties.content.write(content); newDoc.properties.content.mimetype = mimetype;
newDoc.save();

Can you please tell this?
…………….

[size=150]
And I strongly request you to see my Other post with name  "HELP: alfresco APIs and Services? "
[/size]

PLEASE I'LL BE VERY VERY THANKFUL TO YOU! I want to complete it as soon as possible.
  • harishns
    Champ in-the-making
    Champ in-the-making
    Hi,
    I have uploaded with this sample html. hope this helps in uploading a file

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
        <title>File Upload</title>
       </head>
    <body>
        <form method="POST" name="uploadForm" enctype='multipart/form-data' id="upload_form" action="http://localhost/alfresco/service/api/upload">
          
            <input type="hidden" name="destination" id="destination" value="workspace://SpacesStore/9305eefb-d062-45de-b135-4c7a505b89ba"/> // node reference of the folder where you want to upload , you can get it from alfresco webclient by view details action
         

            <label>
                Name:
            </label>
            <br/>
            <input type="text" name="filename" id="filename"/><p/>
            <label>
                Description:
            </label>
            <br/>
            <textarea name="description" id="description"></textarea>

            <p/>
            <label>
                File:
            </label>
            <br/>
            <input class="button" type="file" name="filedata" id="filedata"/><p/>
          <input class="button" type="submit" name="submit" value="Upload"/>
        </form>
    </body>
    </html>

    groberts
    Champ on-the-rise
    Champ on-the-rise
    Thanks.  That helped me investigate a problem I am having calling this Web Script from Ruby on Rails (not a browser).