Can someone explain how to implement the jQuery File Upload plugin?

前端 未结 10 558
一向
一向 2020-12-02 04:44

EDIT (Oct 2019):

6 years later and jQuery File Upload is clearly still driving folks insane. If you\'re finding little solace in the answers here, try a se
10条回答
  •  执笔经年
    2020-12-02 05:09

    I was looking for a similar functionality some days back and came across a good tutorial on tutorialzine. Here is an working example. Complete tutorial can be found here.

    Simple form to hold the file upload dialogue:

    And here is the jQuery code to upload the files:

    $('#upload').fileupload({
    
      // This function is called when a file is added to the queue
      add: function (e, data) {
        //This area will contain file list and progress information.
        var tpl = $('
  • '+ ''+ '

  • ' ); // Append the file name and file size tpl.find('p').text(data.files[0].name) .append('' + formatFileSize(data.files[0].size) + ''); // Add the HTML to the UL element data.context = tpl.appendTo(ul); // Initialize the knob plugin. This part can be ignored, if you are showing progress in some other way. tpl.find('input').knob(); // Listen for clicks on the cancel icon tpl.find('span').click(function(){ if(tpl.hasClass('working')){ jqXHR.abort(); } tpl.fadeOut(function(){ tpl.remove(); }); }); // Automatically upload the file once it is added to the queue var jqXHR = data.submit(); }, progress: function(e, data){ // Calculate the completion percentage of the upload var progress = parseInt(data.loaded / data.total * 100, 10); // Update the hidden input field and trigger a change // so that the jQuery knob plugin knows to update the dial data.context.find('input').val(progress).change(); if(progress == 100){ data.context.removeClass('working'); } } }); //Helper function for calculation of progress function formatFileSize(bytes) { if (typeof bytes !== 'number') { return ''; } if (bytes >= 1000000000) { return (bytes / 1000000000).toFixed(2) + ' GB'; } if (bytes >= 1000000) { return (bytes / 1000000).toFixed(2) + ' MB'; } return (bytes / 1000).toFixed(2) + ' KB'; }

    And here is the PHP code sample to process the data:

    if($_POST) {
        $allowed = array('jpg', 'jpeg');
    
        if(isset($_FILES['uploadctl']) && $_FILES['uploadctl']['error'] == 0){
    
            $extension = pathinfo($_FILES['uploadctl']['name'], PATHINFO_EXTENSION);
    
            if(!in_array(strtolower($extension), $allowed)){
                echo '{"status":"error"}';
                exit;
            }
    
            if(move_uploaded_file($_FILES['uploadctl']['tmp_name'], "/yourpath/." . $extension)){
                echo '{"status":"success"}';
                exit;
            }
            echo '{"status":"error"}';
        }
        exit();
    }
    

    The above code can be added to any existing form. This program automatically uploads images, once they are added. This functionality can be changed and you can submit the image, while you are submitting your existing form.

    Updated my answer with actual code. All credits to original author of the code.

    Source: http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/

提交回复
热议问题