2012-01-06 3 views
0

나는 JQuery에 익숙하지 않으며 나는 그것에 대해 아무것도 모른다고 말할 것이다. 나는 AJAX를 포함하는 작은 이미지 업 로더를 개발하고 싶었다. 구글에서이 코드 조각을 얻었고 제대로 작동하도록했습니다. 이제 파일을 업로드하는 동안 임의의 이미지 이름을 사용해야합니다. 내가 가지고있는 코드는 이미지 자체의 파일 이름으로 업로드하고 있습니다. 필자는 파일 명명 개념을 변경하기 위해 어디를보아야할지 모릅니다. 그래서 시작 지점부터 추적하기로 결정했습니다. 문제를 좁히는 데 도움을 줄 수 있습니까? 그래서 일부 JQuery도 배울 것입니다. 여기에 코드가 있습니다.JQuery - 다음 코드는 어떻게 작동합니까?

$(function(){ 
    $('#swfupload-control').swfupload({ 
     upload_url: "upload-file.php?p_id=<?php echo $prod_id; ?>", 
     file_post_name: 'uploadfile', 
     file_size_limit : "1024", 
     file_types : "*.jpg;*.png;*.gif", 
     file_types_description : "Image files", 
     file_upload_limit : 5, 
     flash_url : "js/swfupload/swfupload.swf", 
     button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png', 
     button_width : 114, 
     button_height : 29, 
     button_placeholder : $('#button')[0], 
     debug: false 
    }) 
    .bind('fileQueued', function(event, file){ 
     //alert(file.name); 
     var listitem='<tr class="r1" id="'+file.id+'" >'+ 
      '<td>&nbsp;</td>'+ 
      '<td><span class="filename"><em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) </span>'+ 
      '<span class="progressvalue" ></span>'+ 
      '<div class="progressbar" ><div class="progress" ></div></div>'+ 
      '<p class="status" >Pending</p></td>'+ 
      '<td>&nbsp;</td>'+ 
      '<td><span class="cancel" >&nbsp;</span></td>'+ 
      '</tr>'; 
     $('#log').append(listitem); 
     $('tr#'+file.id+' .cancel').bind('click', function(){ 
      var swfu = $.swfupload.getInstance('#swfupload-control'); 
      swfu.cancelUpload(file.id); 
      $('tr#'+file.id).slideUp('fast'); 
     }); 
     // start the upload since it's queued 
     //alert(this.getAttribute("id")) 
     $(this).swfupload('startUpload'); 
    }) 
    .bind('fileQueueError', function(event, file, errorCode, message){ 
     alert('Size of the file '+file.name+' is greater than limit'); 
    }) 
    .bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){ 
     $('#queuestatus').text('Files Selected: '+numFilesSelected+'/Queued Files: '+numFilesQueued); 
    }) 
    .bind('uploadStart', function(event, file){ 
     $('#log tr#'+file.id).find('p.status').text('Uploading...'); 
     $('#log tr#'+file.id).find('span.progressvalue').text('0%'); 
     $('#log tr#'+file.id).find('span.cancel').hide(); 
    }) 
    .bind('uploadSuccess', function(event, file, serverData){ 
     var item=$('#log tr#'+file.id); 
     var content = '<td><img src="images/ProductImages/'+file.name+'" width="50" height="50"/><td/>' 
      +'<td>'+file.name+'</td> <td style="text-align: center;">' 
      +'<input type="checkbox" value="'+file.name+'" name="main" onClick="update_chk(this.value);"/></td>' 
      +'<td>Delete</td>'; 
     item.html(content); 

    }) 
    .bind('uploadProgress', function(event, file, bytesLoaded){ 
     //Show Progress 
     var percentage=Math.round((bytesLoaded/file.size)*100); 
     $('#log tr#'+file.id).find('div.progress').css('width', percentage+'%'); 
     $('#log tr#'+file.id).find('span.progressvalue').text(percentage+'%'); 
    }) 

    .bind('uploadComplete', function(event, file){ 
     // upload has completed, try the next one in the queue 
     $(this).swfupload('startUpload'); 
    }) 

}); 
function update_chk(value){ 
    document.getElementById("main_name").value = value; 
} 
+1

당신은 당신의 서버 측 스크립트에 임의의 하나에 파일 이름을 변경해야하지 않나요? 그것은 가장 쉬운 일입니다. – JohnP

+0

@JohnP : 예. 업로드자가 업로드 상태를 나열하면 업로드 한 이미지의 미리보기 이미지를로드합니다. 클라이언트 측에서 알지 못하는 이미지의 임의의 이름을 사용하는 경우 임의의 이름. 임의의 이름 변경을 클라이언트 측에 어떻게 통지합니까? – Deepak

+0

은 어떻게하는지에 따라 다릅니다. 업로드를 시작하기 전에 상태를 되돌릴 수 있습니다. 어쩌면 두 단계로 진행될 수 있습니다. 어쨌든 서버에서 가져온 정보를 사용하여 축소판 및 진행 상황을 표시하는 이유는 무엇입니까? 이건 내가 아는 한 고객으로부터 오는 것입니다. – JohnP

답변

2

fileupload, PHP, Java에 사용하는 서버 측 기술은 무엇입니까? 서버 측에서는 임의 문자열을 생성하여 기본 문자열 대신 파일 이름으로 사용할 수 있습니다.

PHP 예를 들어 하나 할 것 :

// Where the file is going to be placed 
$target_path = 'uploaded_files/'; 

/* Add the original filename to our target path. 
Result is "uploaded_files/filename.extension" */ 
$target_path = $target_path . basename(md5($_FILES['file']['name'])); 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['file']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
+0

내 질문에 대한 내 답글 덧글 확인 ... – Deepak

관련 문제