cancel
Showing results for 
Search instead for 
Did you mean: 

File Upload, Node Content Empty

oweno
Champ in-the-making
Champ in-the-making
I am using a simple PHP script to test file upload service.  Works great on my localhost (Ubuntu 9.04 / Alfresco 3.2, community), but on another server (Windows, Alfresco 3.1.1 enterprise), the node gets created fine with correct name, description, etc, but the node content is empty (no file).    wondering if its a problem with the version, 3.2  vs 3.1

   trying to figure out what is going wrong, nothing in logs, no exceptions thrown. Both Alfresco installs (my local, and remote windows) are vanilla, and share only a few customizations.
 
 

// Include the required Alfresco PHP API objects 
require_once 'Alfresco/Service/WebService/WebServiceFactory.php';
require_once 'Alfresco/Service/Repository.php';
require_once 'Alfresco/Service/SpacesStore.php';
require_once "Alfresco/Service/Session.php";

$timestamp = time();

// Specify the connection details
// this works:
//$repositoryURL = "http://localhost:8080/alfresco/api";
//$userName = "admin";
//$password = "admin";

// this doesn't work,  node is created, but node content is empty
$repositoryURL = "https://<windows remote machine>:8443/alfresco/api";
$userName = "admin";
$password = "admin";

// Authenticate the user and create a session
$repository = new Repository($repositoryURL);
$ticket = $repository->authenticate($userName, $password);
$session = $repository->createSession($ticket);

// Create a reference to the 'SpacesStore'
$spacesStore = new SpacesStore($session);

$nodes = $session->query($spacesStore, "PATH:\"app:company_home\"");
$contentNode = $nodes[0];  

//create new file
$file_name = $_FILES['uploadedfile']['name'] . " (" . $timestamp . ")";
$file_tmp = $_FILES['uploadedfile']['tmp_name'];
$file_type = $_FILES['uploadedfile']['type'];
$file_size = $_FILES['uploadedfile']['size'];

$upload = $contentNode->createChild('cm_content', 'cm_contains', $file_name);
$contentData = new ContentData($upload, "cm:content");

$contentData->mimetype = $file_type;
$upload->cm_name = $file_name;
$upload->cm_description = 'Test File Description';
$contentData->size = $file_size;
$contentData->encoding = 'UTF-8';
$contentData->writeContentFromFile($file_tmp);
$upload->cm_content = $contentData;

echo "Content set<br>";

try {
  $session->save();
  echo "Saved changes to " . $upload->getId() . "<br>";
} catch (Exception $e) {
  echo 'Caught exception: ';
  print_r($e);
}
4 REPLIES 4

gman
Champ in-the-making
Champ in-the-making
Similar issue here with Community Edition 3.3g. The file upload appears successful and the node is created, but the node content is empty. So it creates the node, but does not populate the node withe the file.

This is what I get when I click on the file:

The node's content is missing:
   node: workspace://SpacesStore/7be83779-40e9-48f5-9726-171b09b7806e
   reader: null 
Please contact your system administrator.

Here is the code for the form:


<html>
   <head></head>
   <body>
      <form enctype="multipart/form-data" action="uploadContent01.php" method="POST">
      <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
      Choose a file to upload: <input name="uploadedfile" type="file" /><br />
      <input type="submit" value="Upload File" />
      </form>
   </body>
</html>

Here is the PHP code:


<?php

   $parantID="0dbd52a9-2d80-4e85-8e41-7bb03be90bed";

   $repoURL = "http://alfrescourl/alfresco/api";
   $userName = "admin";
   $password = "password";

   $repository = new Repository($repoURL);

   $ticket = $repository->authenticate($userName,$password);
   $session = $repository->createSession($ticket);

   $spacesStore = new SpacesStore($session);
   $companyHome = $spacestore->companyHome;

   $currentNode = null;

   $currentNode=$session->getNode($spacesStore,$parantID);

   $upload = $currentNode->createChild('cm_content', 'cm_contains', 'cm_'.$_FILES['uploadedfile']['name']);
   $pathFile = $_FILES['uploadedfile']['tmp_name'];

   $content_data = new ContentData($upload, $property, $mimetype, $encoding);
   $content_data->mimetype = $_FILES['uploadedfile']['type'];
   $content_data->encoding = "UTF-8";
   $content_data->writeContentFromFile($pathFile);

   $upload->cm_content = $content_data;

   $upload->cm_name = $_FILES['uploadedfile']['name'];

   echo $upload->cm_name;
   $session->save();
?>

gmasand09
Confirmed Champ
Confirmed Champ

what is parantID ?? why it  is used ? can you please me 

gyro_gearless
Champ in-the-making
Champ in-the-making
I suppose it is not enough to create a ContentData object, you also have to set it as cm:content property of the node!
Here is some code snippet i found:

// Create new node
$newNode = $contentNode->createChild('cm_content', 'cm_contains', "Hui_Buh__$timestamp");

// Create contentdata from some file
$contentData = new AlfContentData($newNode, '{http://www.alfresco.org/model/content/1.0}content');
$contentData->setMimetype('application/pdf');
$contentData->setEncoding ('UTF-8');
$contentData->writeContentFromFile('4046.pdf');

// Set the properties on node
$props = $newNode->getProperties();
$props['{http://www.alfresco.org/model/content/1.0}content'] = $contentData;
$props['{http://www.alfresco.org/model/content/1.0}name'] = "Hui Buh (" . $timestamp . ").pdf";
$props['{http://www.alfresco.org/model/content/1.0}description'] = 'Ein doofer Test';
$props['{http://www.alfresco.org/model/content/1.0}title'] = 'Alles ueber Hui Buh';
$newNode->setProperties($props);

// Save it all
$session->save(false);



HTH
Gyro

gman
Champ in-the-making
Champ in-the-making
I gave up trying to do file upload with Alfresco PHP integration. I simply cannot figure out how to get beyond the empty node.