HTTP Uploading

Overview

This section describes the HTTP uploading methods supported by the Flix Publisher plug-ins. It's broken up into two subsections:

Modes

This section describes the two HTTP uploading modes supported by the Flix Publisher API.

Form POST

The plug-ins use an implementation based upon the method outlined in RFC 1867: Form-based File Upload in HTML.

These are the basic steps involved in initiating a HTTP form upload using the Flix Publisher plug-ins:

Binary POST

Note:
Binary POST is not possible on the Mac.
The Flix Publisher plug-ins can upload files to web server using binary POST as well. In a binary POST, the uploaded file is the body of the request.

These are the basic steps involved in initiating a HTTP form upload using the Flix Publisher plug-ins:

Reference Application Configuration

To learn about setting up the reference application for HTTP uploading, try the following links:




Script Examples

This section contains example code that is taken directly from the reference application. The client side script examples were taken from flixpub_main.js.

The server side script, upload.php, demonstrates how one might handle form uploads on the server: Server PHP Example

Client Javascript Examples

This section describes the example uploading code included in the reference application. All of the examples assume that fileEncoded() or captureCompleted() have been fired. While this assumption is made here and throughout the reference application, it's important to note that uploadFile() can be used without first creating a file.

Example 1: fp_set_upload_settings()

The function fp_set_upload_settings can be found in flixpub_main.js. It is used by the reference application for all uploading.

function fp_set_upload_settings(pub) {
    var t = new Date();
    var upload_file_name = upload_username
        + "_"
        + String(t.getFullYear())
        + String(1+t.getMonth())
        + String(t.getDate())+String(t.getHours())
        + String(t.getMinutes())
        + String(t.getSeconds())
        + ".flv";
    t = 0;

    pub.PublishMethod = upload_mode;

    if (upload_mode == kUploadModeFtp) {
        // FTP upload
        pub.FtpSubDomain       = upload_sub_domain;
        pub.FtpMode            = "P";
        pub.FtpDestinationFile = upload_path_ftp + upload_file_name;
        pub.FtpUser            = upload_username;
        pub.FtpPassword        = upload_password_ftp;
    } else {
        // HTTP POST upload
        
        // set the variables/headers from the conf file
        try {
            fp_set_http_maps(pub, upload_mode);
        } catch (e) {};
        
        try {
            fp_set_md5_checksum(pub, upload_mode, upload_file_name);
        } catch (e) {};
    
        try {
            // backwards compatibility: this was done prior to v3.0.1.3
            //  some implementations of the publisher rely upon this behavior...
            if (pub.getHttpFormVar("username") == "") {
                pub.setHttpFormVar("username", upload_username);
            }
            if (pub.getHttpFormVar("filename") == "") {
                pub.setHttpFormVar("filename", upload_file_name);
            }
        } catch (e) {};

        pub.HttpSubDomain = upload_sub_domain;
        
        if (upload_http_target_is_file == true) 
            pub.HttpPath = upload_file_name;
        else 
            pub.HttpPath = upload_path_http;
    }
}

Example 2: fp_set_md5_checksum()

This example demonstrates how the reference application gets the MD5 checksum of the output file, and includes it within the POST as either form data or request header based on the value of upload_md5_form_var.

function fp_set_md5_checksum(pub, mode) {
    if (mode == kUploadModeFtp || upload_md5_enable == false) return;
    
    var cs;
    try {
      cs = pub.getMd5Checksum(pub.OutputFileName);
    } catch (e) {};
    
    if (cs.length) {
        if (mode == kUploadModeHttpFormPost && upload_md5_form_var == true) {
            pub.setHttpFormVar(upload_md5_name, cs);
        } else {
            pub.setHttpRequestHeader(upload_md5_name, cs);
        }
    }
}

Example 3: fp_start_upload()

This little function takes care of initiating the upload. The reference application calls the function for all uploads.

Note:
fp_start_upload() assumes that fp_set_upload_settings() was called first.
function fp_start_upload() {
    try {
        fp_set_upload_settings(on2pub);
        on2pub.uploadFile(on2pub.OutputFileName, upload_mode);
    } catch (e) {
        return false;
    }
    return true;
}

Server PHP Example

This section contains the example server side script, upload.php.

The following source code block contains the entire contents of the file. The file itself is included with the SDK in the samples directory.

Note:
For all form file uploads initiated by the Flix Publisher plug-ins, the form's name is 'uploadedfile'.
For more information about upload.php, please read the comments within:

00001 <?php
00002 //=============================================================================
00003 // upload.php
00004 //  File:  $Workfile: upload.php$
00005 //         $Revision: 1$
00006 //
00007 //  Date:  $DateUTC: 2007-03-29 19:28:24Z$
00008 //  
00009 //  Brief: Simple PHP example of capturing and storing a file uploaded by 
00010 //         on2 flix publisher sample # 3 with HTTP uploading enabled
00011 //
00012 //  Info:  http://on2.com/cms-data/pdf/publisher/
00013 //         http://on2.com/cms-data/pdf/publisher/httpuploadpage.html
00014 //
00015 //=============================================================================
00016 
00017 // uploads_directory MUST BE SET TO A DIRECTORY THAT PHP CAN WRITE TO
00018 $uploads_directory = "/http-uploads/";
00019 
00020 
00021 // This script relies upon three predefined PHP "superglobal" variables:
00022 // 1. $_FILES
00023 // 2. $_GET
00024 // 3. $_POST
00025 // For basic information regarding the usage of these variables, please see:
00026 // http://www.php.net/manual/en/reserved.variables.php
00027 
00028 
00029 // To use this script, the client script must specify action=upload in the
00030 // query string.  Extract the action value from the $_GET array.
00031 $action = $_GET['action'];
00032 
00033 // Default result/error string (this will get sent back to the client if the 
00034 // upload action is not specified):
00035 // NOTE: This is optional, you do *not* need to return an informative response 
00036 //       to the client.  The responses in this example code are sent back to the 
00037 //       client in order to help you trouble shoot problems with your uploading 
00038 //       code.
00039 $result = "client attempting upload did not specify upload action!\n";
00040 
00041 // Do you wish to send extended diagnostic information with the response?
00042 // If enabled, the diagnostic information can be viewed in one of two ways:
00043 //  1. Turn logging on in the publisher plug-ins - the server's response will 
00044 //     be included in the log file when HTTP uploading is enabled.
00045 //  2. If using the v3 reference application, set fp_debug to true to add a
00046 //     debug_str(on2pub.getHttpResponse()); to the function uploadFailed() and 
00047 //     uploadComplete().
00048 //  3. Using a packet sniffer, view the response to the POST initiated
00049 //     by the call to publishFile() on the publisher interface (the sniff
00050 //     would need to be run on the client side)
00051 // NOTE: This is optional.
00052 $include_debug_info_in_response = 1;
00053 
00054 if ($action == "upload") {
00055     // The following comming assume that the v3 reference application was used
00056     // to upload the file. 
00057     
00058     
00059     // The username entry in the $_POST array came from a call to setHttpFormVar 
00060     // in the javascript function fp_set_upload_settings(), which is in the file
00061     // flixpub_main.js
00062     $username = $_POST['username']; 
00063     
00064     // Same as above, but the filename form var this time:
00065     $fname_on_server_suggested = $_POST['filename'];
00066 
00067     // The publisher plug-ins submit multipart/form-data using the method 
00068     // described in RFC 1867: Form-based File Upload in HTML.  The RFC can be
00069     // viewed at: http://www.ietf.org/rfc/rfc1867.txt
00070     // The publisher plug-ins submit output files using form input of type 
00071     // 'file' named 'uploadedfile'.  To access the file and information about 
00072     // it, use the 'uploadedfile' entry of the $_FILES array.  The uploadedfile 
00073     // entry is an array with the following fields:
00074     // [name] => The original name of the file on the client machine.
00075     // [type] => The mime type of the file.  Always 'application/octet-stream' 
00076     //           for uploads from the publisher plug-ins.
00077     // [tmp_name] => The temporary filename of the file in which the uploaded 
00078     //               file was stored on the server.
00079     // [error] => upload success/error code.  0 means success.  For more 
00080     //            information, see: 
00081     //            http://www.php.net/manual/en/features.file-upload.errors.php
00082     // [size] => The size, in bytes, of the uploaded file.
00083     
00084     // extract the client side file name:
00085     $fname_on_user_system = $_FILES['uploadedfile']['name'];
00086     
00087     // change .'s to -'s in the client file name (this is purely cosmetic)
00088     $fname_on_user_system = str_replace(".", "-", $fname_on_user_system);
00089     
00090     // construct the output file name and path for the call to move_uploaded_file()
00091     $output_file = $uploads_directory . $username . "__" 
00092         . $fname_on_user_system . "__" . $fname_on_server_suggested;
00093     
00094     // move the file to uploads_directory:
00095     if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $output_file)) {
00096         // success
00097         $result = "File successfully uploaded.\n";
00098     } else {
00099         // failure
00100         $result = "Couldn't write uploaded file to uploads directory!\n";
00101     }
00102 }
00103 
00104 echo "\n" . $result . "\n"; 
00105 
00106 if ($include_debug_info_in_response) {
00107     echo "debug info:\n";
00108     
00109     echo '$_FILES:' . "\n";
00110     print_r($_FILES);
00111     echo "\n";
00112     
00113     echo '$_GET:' . "\n";
00114     print_r($_GET);
00115     echo "\n";
00116     
00117     echo '$_POST:' . "\n";
00118     print_r($_POST);
00119     echo "\n";
00120 }
00121 
00122 ?> 
00123 

On2 Flix Publisher documentation, generated on Mon Jan 7 18:13:01 2008 by doxygen 1.5.4 On2 Technologies, Inc