Kendo

  • UI Framework
  • Mobile
  • Demos
  • Roadmap
  • What's New
  • Download

Skin

  • Framework
  • UI Widgets
  • Integration

    Information

    The Upload is able to upload files out-of-band using the HTML5 File API with fallback for legacy browsers.

    You need to configure save action that will receive the uploaded files. An optional remove action is also available.

    Note: Security restrictions in Chrome prevent this example from working when the page is loaded from the file system (via file:// protocol).

    • Description
    • View Code
    • Configuration (4)
    • Methods (3)
    • Events (7)

    The Upload widget uses progressive enhancement to deliver the best possible uploading experience to users without requiring any extra developer effort. Upload is packed with features, including:

    • Asynchronous and synchronous (on form submit) file upload
    • Multiple file selection
    • Removing uploaded files
    • Progress tracking *
    • File Drag-and-Drop *
    • Cancelling upload in progress *

    * These features are automatically enabled if supported by the browser.

    Upload is a standards-based widget. No plug-ins required.

    Getting Started

    There are two primary ways to configure Upload:

    1. For synchronous upload using an HTML form and input
    2. For asynchronous upload using a simple HTML input

    The async upload is implemented using the new HTML5 File API, but it will gracefully degrade and continue to function in legacy browsers (using a hidden IFRAME). If placed inside a form, queued and partially uploaded files will be sent synchronously if the async upload is submitted with the form.

    Configuring for synchronous upload

    1. Create a simple HTML form and input element of type "file"

    <!-- Kendo will automatically set the proper FORM enctype attribute -->
    <form method="post" action="handler.php">
        <div>
            <input name="files" id="files" type="file" />
        </div>
    </form>

    2. Initialize Upload with a jQuery selector

       $(document).ready(function() {
           $("#files").kendoUpload();
       });

    It’s important to note that some type of server-side handler is needed to process and save the uploaded files. There are different server-side techniques for handling file uploads depending on the technology you use. Please consult the documentation for your server technology to understand how to implement a basic file handler.

    Configure for async upload

    1. Create a simple HTML input of type "file" (no HTML form is required*)

    <input name="files" id="files" type="file" />

    2. Initialize Upload and configure async upload end-points

    $("#files").kendoUpload({
        async: {
            saveUrl: "saveHandler.php",
            removeUrl: "removeHandler.php",
            autoUpload: true
        }
    });

    Like synchronous uploads, the async upload requires a server-side handler to process and save (or remove) the uploaded files. The handlers need to accept POST requests. The save action will POST the file upload to the handler (similar to synchronous uploads). The remove action will POST only the name of the file that should be removed on the server.

    Both handlers should return either:

    • An empty response to signify success.
    • Response containing JSON string with "text/plain" content encoding. The deserialized object can be accessed in the success event handler.
    • Any other response to signify error.

    Configuring Upload behavior

    Upload enables most behaviors by default, providing the richest experience possible depending on browser capabilities. Behaviors can be easily configured, though, using simple configuration properties. Refer to the Upload demo Configuration tab for more information on available properties.

    Disable Upload default behaviors

    $("#upload").kendoUpload({
        multiple: false,
        showFileList: false
    });
    No code
    async: Object
    Configures the upload for asynchronous operation.
    saveUrl: (String)
    The URL of the handler that will receive the submitted files. The handler must accept POST requests containing one or more files with the same name as the upload component.
    removeUrl: (String)
    The URL of the handler responsible for removing uploaded files (if any). The handler must accept POST requests containing one or more "fileNames" fields specifying the files to be deleted.
    removeVerb: (String)
    The HTTP verb to be used by the remove action. The default value is "DELETE".
    autoUpload: (Boolean)
    The selected files will be uploaded immediately by default. You can change this behavior by setting autoUpload to false.

    The save and remove handlers should return either:

    • An empty response to signify success.
    • Response containing JSON string with "text/plain" content encoding. The deserialized object can be accessed in the success event handler.
    • Any other response to signify error.

    Fallback to synchronous upload

    The Upload has a fallback mechanism when it is placed inside a form and is configured for asynchronous operation. Any files that were not fully uploaded will be sent as part of the form when the user submits it. This ensures that no files will be lost, even if you do not take any special measures to block the submit button during upload.

    You have to handle the uploaded files both in the save handler and in the form submit action.

    enabled: Boolean(default: true)
    Can be used to disable the upload. A disabled upload can be enabled by calling enable().
    multiple: Boolean(default: true)
    Enables or disables multiple file selection. If set to false, users will be able to select only one file at a time. Note: This option does not limit the total number of uploaded files in asynchronous configuration.
    showFileList: Boolean(default: true)
    Controls whether to show the list of uploaded files. Hiding the list can be useful when you want to fully customize the UI. Use the client-side events to build your own UI.
    • disable ()
      Disables the upload.

      Example

      var upload = $("#upload").data("kendoUpload");
      // disables the upload
      upload.enable();
    • enable ()
      Enables the upload.

      Example

      var upload = $("#upload").data("kendoUpload");
      // enables the upload
      upload.enable();
    • toggle (enable)
      Toggles the upload enabled state.

      Example

      var upload = $("#upload").data("kendoUpload");
      // toggles the upload enabled state
      upload.toggle();

      Parameters

      enable: Boolean
      (Optional) The new enabled state.
    cancel
    Fires when the upload has been cancelled while in progress.

    Event data

    files : Array
    List of the files that were uploaded or removed . Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    complete
    Fires when all active uploads have completed either successfully or with errors.
    error
    Fires when an upload / remove operation has failed.

    Event data

    files : Array
    List of the files that were uploaded or removed . Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    operation : String
    - "upload" or "remove".
    XMLHttpRequest : Object
    This is either the original XHR used for the operation or a stub containing:
    • responseText
    • status
    • statusText
    Verify that this is an actual XHR before accessing any other fields.
    remove
    Fires when an uploaded file is about to be removed. Cancelling the event will prevent the remove.

    Event data

    files : Array
    List of the files that were uploaded or removed . Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    data : Object
    - undefined by default, but can be set to a custom object to pass information to the save handler.
    select
    Fires when one or more files are selected. Cancelling the event will prevent the selection.

    Event data

    files : Array
    List of the selected files. Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    success
    Fires when an upload / remove operation has been completed successfully.

    Event data

    files : Array
    List of the files that were uploaded or removed . Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    operation : String
    - "upload" or "remove".
    response : String
    - the response object returned by the server.
    XMLHttpRequest : Object
    This is either the original XHR used for the operation or a stub containing:
    • responseText
    • status
    • statusText
    Verify that this is an actual XHR before accessing any other fields.
    upload
    Fires when one or more files are about to be uploaded. Cancelling the event will prevent the upload.

    Event data

    files : Array
    List of the files that will be uploaded. Each file has:
    • name
    • extension - the file extension inlcuding the leading dot - ".jpg", ".png", etc.
    • size - the file size in bytes (null if not available)
    data : Object
    - undefined by default, but can be set to a custom object to pass information to the save handler.
    • Home
    • UI Framework
    • Mobile
    • Demos
    • Roadmap
    • What's New
    • Blogs
    • Forums
    • Documentation
    • FAQ
    • About
    • Follow us on Twitter
    • Subscribe to our RSS feed

    Kendo UI is powered by Telerik

    Copyright © 2011 Telerik Inc. All rights reserved.