Previous page

Next page

Locate page in Contents

Print this page

Uploading a File

The filerBinding class provides the upload method for uploading files to the Hardware Node or a Virtuozzo Container. The maximum block size that you can upload in a single method invocation is 512 kilobytes. If uploading a file larger than 512K, you'll have to read the file data in 512K or smaller blocks and transfer each one individually, i.e. one method invocation per data block. The first method invocation creates a file on the destination server and adds the first data block to it. The rest of the source data can be appended to or inserted at the desired position in the destination file. The following sample shows how to upload a file larger than 512K in size.

/// <summary>

/// Sample function UploadFile.

/// Shows how to upload a file to a Virtuozzo Container.

/// </summary>

/// <param name="source">Source file name and path.</param>

/// <param name="destination">

/// Destination file name and path.

/// </param>

/// <param name="eid">Server ID of the target Container.</param>

/// <returns>"OK" or error info.</returns>

/// <example>

/// <para>

/// Example: UploadFile(@"c:\share\packets.txt",@"/tmp/pack15")

/// </para>

/// </example>

///

public string UploadFile(string source, string destination, string eid)

{

    try {

        filerBinding filer = (filerBinding)binder.InitBinding(typeof(filerBinding), eid);

    

        /* An offset in the destination file at which

         * to insert the data. The value of -1 means

         * to append the data to the file.

         */

        int offset = -1;

    

        // Set the read buffer size (in bytes).

        // The maximum possible block size is 512K.

        long readBlockSize = 524288;

    

        // The main input object.

        upload upload_input = new upload();

    

        /* You can upload more than one file in a single call.

         * Here we upload just one file.

         */

        upload_input.file = new uploadFile[1];

    

        // Initialize the wrapper for the source file path.

        FileInfo info = new FileInfo(source);

    

        // Open the file for reading.

        FileStream fstream = new FileStream(source, FileMode.Open, FileAccess.Read);

    

        // Read the file data in 512K blocks.

        long remaining_bytes;

        while ((fstream.Position < fstream.Length)) {

    

            remaining_bytes = fstream.Length - fstream.Position;

    

            // Initialize the buffer for the current data block.

            byte[] readBlock;

            if (remaining_bytes > readBlockSize) {

                readBlock = new byte[readBlockSize];

            }

            else {

                readBlock = new byte[remaining_bytes];

            }

    

            // Read the data block into the buffer.

            fstream.Read(readBlock, 0, readBlock.Length);

    

            /* When adding data to an existing file,

             * the "force" option must be set. Otherwise, the

             * upload will fail.

             */

            upload_input.force = new object();

    

            // Set the offset at which to start writing.

            upload_input.file[0] = new uploadFile();

            upload_input.file[0].offsetSpecified = true;

            upload_input.file[0].offset = offset;

    

            // Set the destination file name and path.

            upload_input.file[0].path = System.Text.ASCIIEncoding.ASCII.GetBytes(destination);

    

            // The block of data to write in this iteration.

            upload_input.file[0].body = readBlock;

            upload_input.file[0].size = readBlock.Length;

    

            // Upload the data block.

            filer.upload(upload_input);

        }

    

        fstream.Close();

        return "OK!";

    }

    catch (Exception e) {

        return "Exception: " + e.Message;

    }

}

Please send us your feedback on this help page