9. Managing Embedded Files

As files can be inserted into edit-on Pro from various sources such as the local file system and the Internet, the editor provides a file upload mechanism which finds all files in the document not located in the file base and allow the user to upload them either by performing HTTP POST requests to your server or using WebDAV if you have a WebDAV-enabled server.

Note:

The file base can be specified in the configuration file using the base element. If this element is specified the editor will search for files with a relative path in a location relative to the base . If the element is not specified, the file base defaults to the code base.

9.1. Uploading and Inserting Files Using WebDAV

To use this technology to transfer files from and to your server, your web server must support the WebDAV protocol. If your server meets this requirement, you can set up edit-on Pro to allow the uploading of any files not located in the file base to a directory on your web server and the insertion of files into the document being editied directly from your file repository. To do so, you will have to activate WebDAV in the configuration file, and enable the according dialogs to upload and insert files.

Example II.21. Enabling upload and insert dialogs

<uiconfig> 
    ... 
    <toolbar> 
        ... 
        <button actionid="UPLOADIMAGES" /> 
        <button actionid="UPLOADOBJECTS" />
        <button actionid="ULOADDOCUMENT" />
        <button actionid="IMAGE" />
        <button actionid="OBJECT" />
        <button actionid="LINK" /> 
        ... 
    </toolbar> 
    ... 
</uiconfig>


The example above shows how to activate the actions for the upload and insertion of files. Each of these dialogs handles a different type of files (images, objects, and the document currently being edited). To enable the browsing of the server repository in the corresponding dialogs, the according repository for this file type must be specified in the configuration file. Here is an overview of which file types are used with which actions and of the respective purpose of each setting:

Table II.1. Overview of ACTIONS and their corresponding configuration

ActionConfiguration SettingPurpose
UPLOADIMAGES<file type="image">Allows to upload images to the specified repository.
UPLOADOBJECTS<file type="object">Allows to upload objects Allows to upload images to the specified repository.
UPLOADDOCUMENT<file type="document">Allows to save the document currently being edited to the specified repository.
IMAGE<file type="image">Allows to insert images from the WebDAV repository.
OBJECT<file type="object">Allows to insert objects from the WebDAV repository.
LINK<file type="link">Allows to insert hyperlinks to the files in the specified WebDAV repository.
LOAD<file type="document">Allows to load a document from the WebDAV repository.


See below for an example on how to configure a repository for each file type.

Example II.22. Activating WebDAV in the configuration file

<filemanagement>
    <file type="image" 
        baseurl="http://www.myserver.com/" 
        autoupload="true"
        onuploadfinished="uploadStatusReport">
        <webdav url="http://www.myserver.com/repository/images" 
            username="user"
            userpassword="mypw"
            debug="true"
            putmethodexpectation="false"
            mimetype="image/gif" />
    </file>
    <file type="object" 
        baseurl="http://www.myserver.com/"
        autoupload="true">
        <webdav url="http://www.myserver.com/repository/multimedia"
        username="user" 
        userpassword="mypw" 
        debug="true" 
        putmethodexpectation="false" />
    </file>
    <file type="document" 
        baseurl="http://www.myserver.com/"> 
        <webdav url="http://www.myserver.com/repository/docs"
        username="someotheruser" 
        userpassword="someotherpw" 
        debug="true"
        putmethodexpectation="false" />
    </file> 
</filemanagement>

In the example above we added three buttons to the editor's toolbar. They respectively open the "Upload Images" dialog, the "Upload Objects" dialog and the "Upload Document" dialog. Thus, we have now configured the editor to allow the user to upload any images and objects not located in the file base to the location "http://www.myserver.com/repository/images" for images or "http://www.myserver.com/repository/objects". As the file base allows the editor to look up images with relative URLs, it is a good idea to set the file base location to the root of the WebDAV server you are using.

Example II.23. Configuring the file base

<config>
    ...
    <base href="http://www.myserver.com/" />
    ...
</config>

An additional benefit of using WebDAV is it will activate an directoryadditional pane in the "Insert Image", "Insert Object" and "Insert Hyperlink" dialogs which will let the user insert images or objects directly from the WebDAV repository and let him browse and place a link to these documents within the "Insert Hyperlink" dialog.

Note:

You can enable a debug mode for WebDAV which logs all actions performed by the WebDAV to the Java console by setting the debug attribute of the webdav element to true.

Important:

The password for the WebDAV connection specified by the userpassword attribute may not contain whitespace. Please make sure it does not contain any whitespace or the connection to the WebDAV repository may fail if a password is required.

9.2. Uploading Files Using HTTP Connections

If your web server does not support WebDAV, you can upload files per HTTP POST. To activate this upload method, you have to configure the editor accordingly.

Example II.24. Activating the HTTP POST upload mechanism in the configuration file

<filemanagement>
    <file type="image" baseurl="http://www.myserver.com/"
        autoupload="true"
        onuploadfinished="uploadStatusReport">
        <http url="http://www.myserver.com/upload.php" />
    </file>
    <file type="object" baseurl="http://www.myserver.com/" 
        autoupload="true">
        <http url="http://www.myserver.com/upload.php" />
    </file>
    <file type="document" baseurl="http://www.myserver.com/" >
        <http url="http://www.myserver.com/upload.php" /> 
    </file>
</filemanagement>

As you can see, the configuration is similar to the configuration used for WebDAV, the main difference being that we do not specify repositories using the webdav element, but instead specify a server-side script to take care of the file upload.

Once the HTTP POST file upload is activated, all files included in the document can be uploaded using the one of the upload dialogs. The files selected for upload images will then be sequentially posted to the server side script handling the file upload specified by the url attribute of the http element in the configuration file.

Each file will be contained in a form field called uploadfile . The server side script must return "OK" if the file upload succeeded and return "Failed" if it failed. In addition, you can return the new absolute path the file will be saved to. The editor will then modify the path of the embedded file in the document.

Example II.25. Sample server side script handling the image upload (PHP)

<?php
$filename = $_FILES["uploadfile"]["name"];
$tmp_name = $_FILES["uploadfile"]["tmp_name"];

if(file_exists($filename)) {
  while(file_exists($filename)) {
    $ext = strchr($filename,".");
    $pos = strpos($filename,".");
   $filename = substr($filename,0,$pos)."$".$ext;
  }
}

if($fp = fopen($tmp_name,"rb")) {
  $file = fread ($fp, filesize($tmp_name));
  fclose($fp);
} else 
  die("Failed");

if($fp = fopen($filename,"wb")) {
  fwrite($fp,$file);
  fclose($fp);
} else 
  die("Failed");

echo "OK";
echo "|http://yourserver.com/eop4".$filename."|";
?>


9.3. Using the JavaScript API to Trigger the File Upload

You can use the JavaScript function uploadToRepository to trigger the file upload. The uploadToRepository function will open the "Upload Images", "Upload Document" or "Upload Document" when it is fired, depending on the file type you selected. The function allows you to specify another JavaScript function as argument, which will receive a hashtable as parameter when the upload is finished. This hashtable will contain status information about each file that was uploaded. It will contain the following info for each file:

  • The old name of the file

  • The new name of the uploaded file

  • The new file path on the server

  • The status code returned by the server

  • The type of the upload that was performed ( image , object or document )

The event handler specified by uploadToRepository will actually receive two arguments: a reference to the applet and the hashtable with upload information.

Example II.26. Using the uploadToRepository() function

function checkForUpload(obj,state) {
    for (i=0;i<state.length;i++) {
        alert("Upload status:\n" 
            +state[i][0] 
            +"\n"+state[i][1] 
            +"\n"+state[i][2]
            +"\n"+state[i][3] 
            +"\n"+state[i][3] 
            +"\n"+state[i][4] 
            +"\n"); 
    } 
}

eop.uploadToRepository("UPLOADIMAGES","checkForUpload");

Note:

Calling the uploadToRepository function inside an event queue will halt the event cycle. I.e., if you are calling any other methods that need to be registered in the queue after the uploadToRepository function, call pumpEvents within the definition of uploadToRepository 's event handler.