cancel
Showing results for 
Search instead for 
Did you mean: 

upload ByteArray into repository

dmatejka
Champ in-the-making
Champ in-the-making
Hallo all,
Im quite new to javascript/webscript and Flex/actionscript programming. What Im trying to do is to upload a ByteArray (ziped xml) into alfrsco repository via webscript from my Flex app. I have a BytteArray on hand but do not know how to "put" it into alfrsco. The best way to do would be through webscript CREATE and its args, but this works only with text.

I was trying to find out how UPLOAD works, but not able to resolve it. Can I send the ByteArray as a parameter or how should I send it?
Some clear and simple example would be REALLY valuable for me.. 

Flexspaces are really wonderful, and I have learned really a LOT out of it.. but cannot move on with this upload issue..  Smiley Sad

anybody help..

thank you..
6 REPLIES 6

stevereiner
Champ in-the-making
Champ in-the-making
clientside: Flex/ActionScript
    You can use URLLoader, do a POST,  with the ByteArray as the data for the URLRequest
serverside
    use a modified upload webscript

http://www.zedia.net/2008/sending-bytearray-and-variables-to-server-side-script-at-the-same-time/
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html

dmatejka
Champ in-the-making
Champ in-the-making
Hi Steve,
thank you a lot for the reply.. I have managed to send ByteArray from flex to my server by following code:

          public function  upload (content:ByteArray):void {
            var url:String = "http://localhost:8080/alfresco/service/xpression/uploadNew";
      
            var myByteArray:ByteArray = content;
      
            var request:URLRequest = new URLRequest(url);
            request.method = URLRequestMethod.POST;
           
            request.data = myByteArray;
           
            var loader:URLLoader = new URLLoader();
      
            loader.load(request)
         }

this is sending the ByteArray out from flex, but I am not able to find out how to reference the ByteArray in the server script side.. Smiley Sad

data or content property are not defined.. can you point me somehow where to look?  Or am I doing this completelly wrong?

Thank you again I really appriciate your help.

stevereiner
Champ in-the-making
Champ in-the-making
The various upload webscripts are geared to processing multipart form data
On the flex side you could use add in using URLVariables to have the form data

var variables:URLVariables = new URLVariables();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
variables.bytearray = myByteArray;
request.data = variables;

on the javascript webscript side change to not have field.isFile check and use the different field name
for each (field in formdata.fields)
{
    switch (String(field.name).toLowerCase())
    {
       case 'bytearray':
          content = field.value;
          break;

Without using form variable, don't know how/if can do in javascript webscript. Guess could do from from a Java webscript

dmatejka
Champ in-the-making
Champ in-the-making
Hi Steve,
I can not say how much I appreciate your time…

unfortunately Im missing something obvious, probably.. I have spend last two days and nights looking for " multipart form data" processing in flex both on the web and on flexspaces upload, but Im still not able to acces formdata on the alfresco javascript side.. Smiley Sad

If I run flexspaces' upload I can clearly see the formdata in javascript debugger  and reference to them..
If I run my code I can only acces the properties of "args", but no formdata are available.. Smiley Sad

if I take args.bytearray and try to upload it as a content I get an exception:
…Can't find method org.alfresco.repo.jscript.ScriptNode$ScriptContentData,write(string)…
on the javascript code line
upload.properties.content.write(content);

My flex code is:

public function  upload(content:ByteArray):void
{
   var url:String = "http://localhost:8080/alfresco/service/xpression/uploadNew";
   var myByteArray:ByteArray = content;
   var loader:URLLoader = new URLLoader();
   loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            
   var request:URLRequest = new URLRequest(url);
   request.method = URLRequestMethod.POST;
               
   var variables:URLVariables = new URLVariables();

      variables.bytearray = myByteArray;

      variables.name = 'name.zip'
      variables.path = '/Company Home/temp/'
      variables.mimetype = "text/plain"
      variables.type = "cm:content"
               
   request.data = variables;
   loader.load(request)
}

If you stil have a patience with my questions I would be really thankful..

stevereiner
Champ in-the-making
Champ in-the-making
1. Tried calling URLLoader with your flex code. Added setting request.contentType = "multipart/form-data";
This gives a non-null formdata object in a javascript webscript. But it didn't have any fields.

2. So think what you need is something that will format the multipart data for you. Saw this (haven't tried it):
http://blog.inspirit.ru/?p=139
http://code.google.com/p/in-spirit/wiki/MultipartURLLoader

3. Note: with flash player 10, can get a 2176 security error if the URLLoader upload is not done in handling directly an event from the user (button click, menu, etc.)

dmatejka
Champ in-the-making
Champ in-the-making
Hi Steve,
thank you so much for your help. You have all my respect that you find time to help on the forum….

I have used Multipart URL Loader from the link you send me. With a bit of tweaking on the server side Im now able to upload my custom made acp with the custom folder structure. Have to clean the code but if somebody is interested I ended up with this flex code:


      import ru.inspirit.net.*;
      
       public function  upload(content:ByteArray, path:String, mtype:String):void {

            var ml:MultipartURLLoader = new MultipartURLLoader();
            ml.addEventListener(Event.COMPLETE, onReady);
            
            // simple string data
            ml.addVariable('path', path);
            
            var currentfilename:String = 'filename.acp'
            
            ml.addFile(content, currentfilename, 'file', mtype);
            ml.load('http://localhost:8080/alfresco/service/xpression/uploadNew');   
       }
      
      public function onReady(e:Event):void
      {
         // Upload Complete

      }

Have to insert ticket variable and solve the situation when the upload fails, but the main part is working. Smiley Happy

Thank you again.