2015-10-01 47 views
1

嘿傢伙我卡在一個點。我有一種情況,我需要上傳縮放的圖像在不同的文件夾中,例如: - 1(小)。小文件夾爲jpg,中文文件夾爲1(中),.jpg和上傳文件夾爲1.jpg。現在下面的代碼將所有3張圖片上傳到不同的文件夾,但是這些文件是blob文件。如何獲得縮放圖像。 (編輯的代碼)下面精細上傳w /冷融合上傳縮放圖像在不同的文件夾

<cfset destinationDirectory = expandpath('/file_uploader/uploads')> 
<cfset counter = 1> 
<cfset uniqueFileName = ""> 
<cfset destinationFilePath = ""> 
<cfset smallfile = ""> 
<cfset mediumfile =""> 
<cfset mainfile= ''> 

<cfscript> 
    variables.validMimeTypes = {'image/jpeg': {extension: 'jpg'} 
           ,'image/png': {extension: 'png'} 
           ,'image/png': {extension: 'gif'} 
           }; </cfscript> 


<cftry>  
    <cfset cnt= 1> 
    <cfset file_name = #qqfilename#>  
    <cfset file_size = #qqtotalfilesize#> 

    <!--- If a file with the same name already exists at the destination ---> 
    <cfif fileExists(destinationDirectory & "/" & file_name)> 

      <!--- Loop up to 100 times to try create a unique file_name ---> 

       <cfset destinationFilePath = destinationDirectory & "/" & counter & "_" & file_name> 

      <!--- Loop up to 100 times to try create a unique filename ---> 
       <cfloop condition="counter LT 100"> 
        <cfset destinationFilePath = destinationDirectory & "/" & counter & "_" & file_name> 

        <cfif fileExists(destinationFilePath)> 
          <cfset counter++> 
        <cfelse> 
          <cfset uniqueFileName = "IMG" & counter & "_" & file_name> 
          <cfbreak/> 
        </cfif> 
       </cfloop> 

    <!--- Filename does not already exist at the destination ---> 
    <cfelse> 
      <cfset uniqueFileName = "IMG" & "_" & file_name> 
      <cfset destinationFilePath = destinationDirectory & "/" & file_name> 
    </cfif> 
      <cfif len(trim(uniqueFileName))> 
        <cfset session.cntr ++> 
        <cfif find("(small)",file_name) neq 0> 
         <cfset smallfile = file_name> 
         <cffile 
           action="upload" 
           destination="#UploadPath2#" 
           accept="#StructKeyList(variables.validMimeTypes)#" 
           nameconflict="#smallfile#" 
           > 
        <cfelseif find("(medium)",file_name) neq 0> 
         <cfset mediumfile = file_name> 
         <cffile 
           action="upload" 
           filefield="qqfile" 
           destination="#UploadPath1#" 
           accept="#StructKeyList(variables.validMimeTypes)#" 
           nameconflict="#uniqueFileName#" 
           > 

        <cfelse> 
         <cfset mainfile= file_name> 
         <cffile 
           action="upload" 
           filefield="qqfile" 
           destination="#destinationFilePath#" 
           accept="#StructKeyList(variables.validMimeTypes)#" 
           nameconflict="#uniqueFileName#" 
           > 
        </cfif> 

        <CFSET local.response['success'] = true > 
        <CFSET local.response['type'] = 'xhr' > 
        <CFSET local.response['obj'] = session.cntr > 


      <cfelse> 
        <CFSET local.response['error'] = 'Unable to move the file and create a unique file_name at the destination'> 
      </cfif> 
    <cfoutput> 
     #serializeJSON(local.response,true)# 
    </cfoutput>   
    <cfabort/> 

<cfcatch type="any">  
    <cfoutput> #serializeJSON(session.cntr,true)# </cfoutput> 
    <cfabort/> 
</cfcatch> </cftry> 

是UI頁

<!DOCTYPE html> <html> <head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <!-- jQuery 
    ====================================================================== --> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
    <!-- Fine Uploader Gallery CSS file 
    ====================================================================== --> 
    <link href="../file_uploader/includes/fine-uploader-gallery.css" rel="stylesheet"> 

    <!-- Fine Uploader JS file 
    ====================================================================== --> 
    <script src="../file_uploader/includes/all.fine-uploader.js"></script> 
    <script src="../file_uploader/includes/all.fine-uploader.min.js"></script> 

    <script src="../file_uploader/includes/jquery.fine-uploader.js"></script> 
    <script src="../file_uploader/includes/jquery.fine-uploader.min.js"></script> 



    <!-- Fine Uploader Gallery template 
    ====================================================================== --> 
    <script type="text/template" id="qq-template-gallery"> 

    <div class="qq-uploader-selector qq-uploader qq-gallery" qq-drop-area-text="Drop files here"> 
    <div class="qq-total-progress-bar-container-selector qq-total-progress-bar-container"> 
       <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-total-progress-bar-selector qq-progress-bar qq-total-progress-bar"></div> 
      </div> 
      <div class="qq-upload-drop-area-selector qq-upload-drop-area" qq-hide-dropzone> 
       <span class="qq-upload-drop-area-text-selector"></span> 
      </div> 
      <div class="qq-upload-button-selector qq-upload-button"> 
       <div>Upload a file </div> 
      </div> 
      <span class="qq-drop-processing-selector qq-drop-processing"> 
       <span>Processing dropped files...</span> 
      <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span> 
      </span> 
      <ul class="qq-upload-list-selector qq-upload-list" role="region" aria-live="polite" aria-relevant="additions removals"> 
       <li> 
        <span role="status" class="qq-upload-status-text-selector qq-upload-status-text"></span> 
        <div class="qq-progress-bar-container-selector qq-progress-bar-container"> 
         <div role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" class="qq-progress-bar-selector qq-progress-bar"></div> 
        </div> 
        <span class="qq-upload-spinner-selector qq-upload-spinner"></span> 
        <div class="qq-thumbnail-wrapper"> 
         <img class="qq-thumbnail-selector" qq-max-size="120" qq-server-scale> 
        </div> 
        <button type="button" class="qq-upload-cancel-selector qq-upload-cancel">X</button> 
        <button type="button" class="qq-upload-retry-selector qq-upload-retry"> 
         <span class="qq-btn qq-retry-icon" aria-label="Retry"></span> 
         Retry 
        </button> 

        <div class="qq-file-info"> 
         <div class="qq-file-name"> 
          <span class="qq-upload-file-selector qq-upload-file"></span> 
          <span class="qq-edit-filename-icon-selector qq-edit-filename-icon" aria-label="Edit filename"></span> 
         </div> 
         <input class="qq-edit-filename-selector qq-edit-filename" tabindex="0" type="text"> 
         <span class="qq-upload-size-selector qq-upload-size"></span> 
         <button type="button" class="qq-btn qq-upload-delete-selector qq-upload-delete"> 
          <span class="qq-btn qq-delete-icon" aria-label="Delete"></span> 
         </button> 
         <button type="button" class="qq-btn qq-upload-pause-selector qq-upload-pause"> 
          <span class="qq-btn qq-pause-icon" aria-label="Pause"></span> 
         </button> 
         <button type="button" class="qq-btn qq-upload-continue-selector qq-upload-continue"> 
          <span class="qq-btn qq-continue-icon" aria-label="Continue"></span> 
         </button> 
        </div> 
       </li> 
      </ul> 

      <dialog class="qq-alert-dialog-selector"> 
       <div class="qq-dialog-message-selector"></div> 
       <div class="qq-dialog-buttons"> 
        <button type="button" class="qq-cancel-button-selector">Close</button> 
       </div> 
      </dialog> 

      <dialog class="qq-confirm-dialog-selector"> 
       <div class="qq-dialog-message-selector"></div> 
       <div class="qq-dialog-buttons"> 
        <button type="button" class="qq-cancel-button-selector">No</button> 
        <button type="button" class="qq-ok-button-selector">Yes</button> 
       </div> 
      </dialog> 

      <dialog class="qq-prompt-dialog-selector"> 
       <div class="qq-dialog-message-selector"></div> 
       <input type="text"> 
       <div class="qq-dialog-buttons"> 
     <button type="button" class="qq-cancel-button-selector">Cancel</button> 
       <button type="button" class="qq-ok-button-selector">Ok</button> 
       </div> 
      </dialog> 
     </div> 
    </script> 

    <title>Fine Uploader Gallery View Demo</title> </head> <body> 

    <!-- Fine Uploader DOM Element 
    ====================================================================== --> 
    <div id="fine-uploader-gallery"></div> 

    <!-- Your code to create an instance of Fine Uploader and bind to the DOM/template 
    ====================================================================== --> 
    <script>  var posturl = "../file_uploader/upload_file.cfm";  var t= 0; 
     var galleryUploader = new qq.FineUploader({ 
      element: document.getElementById("fine-uploader-gallery"), 
      //debug: true, 
      autoUpload: false, 
      template: 'qq-template-gallery', 
      request: 
      { 
       // endpoint: '../file_uploader/upload_file.cfm?id=<cfoutput>#session.cntr#</cfoutput>' 
       endpoint: posturl 
      }, 
      thumbnails: 
      { 
       placeholders: 
       { 
        waitingPath: '../file_uploader/includes/placeholders/waiting-generic.png', 
        notAvailablePath: '../file_uploader/includes/placeholders/not_available-generic.png' 
       } 
      }, 
      validation: 
      { 
       allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'] 
      }, 

      callbacks:   { 
       onComplete: function(id, name, responseJSON , xhrOrXdr) 
       { 

         if (responseJSON.success) 
         { 
          t++; 
         alert("success"+t); 
         uploadSuccess: { 
          endpoint: "../file_uploader/upload_file.cfm?id=1" 
           } 
         } 
       }, 

       onError: function(id, name, errorReason, xhrOrXdr) 
       { 
        alert(qq.format("Error on file number {} - {}. Reason: {}", id, name, errorReason)); 
       } 
      }, 

      scaling: 
      { 
       //hideScaled: true, 
       sizes: 
       [ 
        {name: "small", maxSize: 100}, 
        {name: "medium", maxSize: 300} 
       ] 
      } 

     }); 
    </script> </body> </html> 
+1

與您的問題無關,但您不需要循環來生成唯一的文件名。附加一個UUID或者getTickCount()的結果應該可以做到。 –

+0

你試過看文件名嗎? –

+0

...和@丹也是正確的。 Fine Uploader已經爲您提供了每個文件的uuid。我不知道爲什麼你選擇了遍歷文件名,添加一個計數器變量,直到名稱是「唯一的」。 –

回答

1

至少有兩種方式來確定圖像的經縮放的大小。一種方法是在圖像的文件名中查找適當的前綴。例如,如果您已經指示Fine Uploader包含短語「small」,那麼您應該在文件名服務器端查找字符串「(small)」。您在your scaling options中指定的任何其他尺寸前綴也是如此。

另一種選擇是查看縮放組中文件的實際大小。組中的每個縮放文件將包含指向原始圖像/文件的UUID的qqparentuuid請求參數。一旦你有所有這些文件,你可以檢查它們的大小,以確定哪一個是小,中,大等。

我強烈建議使用第一個選項。

+0

有什麼辦法只顯示小圖像而不顯示其他縮放圖像?謝謝 –

相關問題