cancel
Showing results for 
Search instead for 
Did you mean: 

Upload file using webscript

shai
Champ in-the-making
Champ in-the-making
Hello,
I'm building a simple cm server, and I want my users to be able to upload content to a certain space (almost always the same one), and to be able to apply a tag to it (categorize the content). I tried to do it using the upload webscript provided in the webscripts samples (http://wiki.alfresco.com/wiki/Web_Scripts_Examples#File_Upload), but I got stuck pretty fast.
Can I do it with web scripts? how?
If not, where can I find information about doing it with JAVA servlets?

I should mention that I'm very new to Alfresco, but it seemed nice and easy..

Thanks,
Shai
4 REPLIES 4

schambon
Champ in-the-making
Champ in-the-making
Hi,

You can definitely do that in webscripts. Where are you getting stuck?

shai
Champ in-the-making
Champ in-the-making
Hi,
I can't figure out how to create the new content in a specified space other than 'company home'…

Shai

schambon
Champ in-the-making
Champ in-the-making
Hi,

If the space is always the same one, then just get its node reference (you can get that in the Explorer UI : more actions > view details, then copy the Alfresco Node Reference link), which should look like: workspace://SpacesStore/8469e812-6383-42f6-9d89-872810d0e547

Then in the script, at the bottom you see:

  // create document in company home for uploaded file
  upload = companyhome.createFile("upload" + companyhome.children.length + "_" + filename) ;
  upload.properties.content.write(content);
  upload.properties.encoding = "UTF-8";
  upload.properties.title = title;
  upload.properties.description = description;
  upload.save()

you need to change this to something like:

  var folder = search.findNode("workspace://SpacesStore/8469e812-6383-42f6-9d89-872810d0e547");
  // create document in folder for uploaded file
  upload = folder.createFile("upload" + folder.children.length + "_" + filename) ;
  upload.properties.content.write(content);
  upload.properties.encoding = "UTF-8";
  upload.properties.title = title;
  upload.properties.description = description;
  upload.save()

Rather than using a hardwritten node ref, you can also use:

var folder = companyhome.childByNamePath("Data Dictionary/Wherever/You/Need");

(note that this is less robust, if someone decides to rename your folder you're stuck).

As for the tagging, you can use the addTag method on ScriptNode; so in the script add in something like:

upload.addTag("my_wonderful_new_tag");

Hope that helps,
Sylvain.

PS you may be interested in reading the documentation: http://wiki.alfresco.com/wiki/JavaScript_API

shai
Champ in-the-making
Champ in-the-making
Thanks a lot! It really helped, just as I was beginning to get too frustrated..
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.