2015-09-28 112 views
3

我試圖顯示縮略圖圖像時,他們上傳到服務器上的輸入字段旁邊。問題是我的功能是在圖像上傳之前嘗試顯示圖像。上傳的圖像只顯示一些縮略圖

我認爲這與在服務器上存在縮略圖之前調用縮略圖有關。有沒有辦法改變我的代碼,以便在完成上傳到服務器之後調用要顯示的圖像?

這裏是我的HTML

<div class="module"> 
      <div class="moduleTitle">Upload Photos</div>    
        <form id="upload" method="post" action="actions/upload.php" enctype="multipart/form-data"> 
        <input type="hidden" name="username" value="<?php echo $username; ?>" id="username"> 
      <div id="drop"> 
       Drop Here 

       <a>Browse</a> 
       <input type="file" name="upl" multiple /> 
      </div> 


      <ul> 
       <!-- The file uploads will be shown here --> 
      </ul> 
       </form> 
      </div> 

      <div class="module"> 
      <div class="moduleTitle">Add Tags to Images</div> 
      <form onSubmit="addTags();return false;" id="tagAddForm"> 
      <ul> 
     <!-- Ths is where the tag inputs will appear --> 
     </ul> 
      <input type="submit" name="login" value="Add Tags" class="submit" id="login"/> 
      </form> 
     </div> 

這裏是我的JS

$(function(){ 

    var ul = $('#upload ul'); 
;; 

    $('#drop a').click(function(){ 
     // Simulate a click on the file input button 
     // to show the file browser dialog 
     $(this).parent().find('input').click(); 
    }); 

    // Initialize the jQuery File Upload plugin 
    $('#upload').fileupload({ 

     // This element will accept file drag/drop uploading 
     dropZone: $('#drop'), 

     // This function is called when a file is added to the queue; 
     // either via the browse button, or via drag/drop: 
     add: function (e, data) { 



     var tpl = $('<li class="working uploaded"><input type="text" value="0" data-width="48" data-height="48"'+ 
       ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>'); 


      // Append the file name and file size  
      tpl.find('p').text(data.files[0].name); 


      //Create an empty container 
      var path = "artistpit/users/" + $("#username").val() + "/"; 
      var $elems = $(); 
      //Cycle thru the files 
      $.each(data.files, function(idx, file) { 
       //Create an input with attrsd 
       var $input = $("<input/>", { 
        'type': 'text', 
        'placeholder': 'separate tags with commas', 
        'name': file["name"] 
       }); 
       //Create list element with an image thumb + append input 
       var $li = $("<li/>", { 
       html: "<img src='" + path + file["name"] + "' width='50' />" 
       }).append($input); 
       //Populate the container with the list item 
       $elems = $elems.add($li); 
      }); 
      //Append all list items 
      $("#tagAddForm > UL").append($elems); 




      // Add the HTML to the UL element 
      data.context = tpl.appendTo(ul); 

      // Initialize the knob plugin 
      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'); 
      } 
     }, 

     fail:function(e, data){ 
      // Something has gone wrong! 
      data.context.addClass('error'); 
     } 

    }); 


    // Prevent the default action when a file is dropped on the window 
    $(document).on('drop dragover', function (e) { 
     e.preventDefault(); 
    }); 

    // Helper function that formats the file sizes 
    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'; 
    } 



}); 
+0

請問您可以發佈您的HTML嗎? – frikinside

+0

我做了它只是爲你:)感謝您的幫助 – wuno

回答

1

如果我理解正確的話,你的問題可以,如果你不更新後添加到DOM縮略圖解決完。

嘗試在上傳回調中添加元素。我爲submit()選項添加回調。

$(function(){ 

    var ul = $('#upload ul'); 
;; 

    $('#drop a').click(function(){ 
     // Simulate a click on the file input button 
     // to show the file browser dialog 
     $(this).parent().find('input').click(); 
    }); 

    // Initialize the jQuery File Upload plugin 
    $('#upload').fileupload({ 

     // This element will accept file drag/drop uploading 
     dropZone: $('#drop'), 

     // This function is called when a file is added to the queue; 
     // either via the browse button, or via drag/drop: 
     add: function (e, data) { 



     var tpl = $('<li class="working uploaded"><input type="text" value="0" data-width="48" data-height="48"'+ 
       ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>'); 


      // Append the file name and file size  
      tpl.find('p').text(data.files[0].name); 


      //Create an empty container 
      var path = "artistpit/users/" + $("#username").val() + "/"; 
      var $elems = $(); 
      //Cycle thru the files 
      $.each(data.files, function(idx, file) { 
       //Create an input with attrsd 
       var $input = $("<input/>", { 
        'type': 'text', 
        'placeholder': 'separate tags with commas', 
        'name': file["name"] 
       }); 
       //Create list element with an image thumb + append input 
       var $li = $("<li/>", { 
       html: "<img src='" + path + file["name"] + "' width='50' />" 
       }).append($input); 
       //Populate the container with the list item 
       $elems = $elems.add($li); 
      });  


      // Add the HTML to the UL element 
      data.context = tpl.appendTo(ul); 

      // Initialize the knob plugin 
      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().success(function(result, textStatus, jqXHR){ 
       //Append all list items 
       $("#tagAddForm > UL").append($elems); 
      }); 
     }, 

     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'); 
      } 
     }, 

     fail:function(e, data){ 
      // Something has gone wrong! 
      data.context.addClass('error'); 
     } 

    }); 


    // Prevent the default action when a file is dropped on the window 
    $(document).on('drop dragover', function (e) { 
     e.preventDefault(); 
    }); 

    // Helper function that formats the file sizes 
    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'; 
    } 



}); 
+0

這造成了錯誤。 $(「#tagAddForm> UL」)。append($ elems);在控制檯它用戶/ nichodiaz/IMG_3666.JPG 404(未找到) – wuno

+0

我假設您在img上建立的src實際上是正確的。如果該src不正確,我什麼也做不了。無論如何,我移動'data.submit()'成功回調的附加項目。一探究竟。 – frikinside

+0

現在我得到了這個Uncaught ReferenceError:$ input is not defined – wuno