2012-01-06 13 views
0

私はJQueryを初めて使っています。私は何も知らないと言います。私は、AJAXに関連する小さな画像アップローダーを開発したかったのです。私はgoogleからこのコードを手に入れました。今は、ファイルをアップロードする際にランダムなイメージ名を使用する必要があります。私が持っているコードは、画像自体のファイル名でアップロードしています。私は実際にどこにファイルの命名の概念を変更するために見てか分からない。だから、私はどこから始めるのかを決めることにした。問題を絞り込む手助けをしてくれますか?だから私はいくつかの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

はあなたのやり方によって異なります。アップロードを開始する前にステータスを返信することができます。たぶんそれを2つのステップのプロセスにしてください。いずれにしても、サムネイルと進行状況を表示するためにサーバーからの情報を使用する理由は何ですか?これは私が知る限りクライアントから来るはずです。 – JohnP

答えて

2

fileupload、PHP、Javaに使用するサーバー側の技術とは何ですか?サーバー側では、ランダムな文字列を生成し、デフォルトの文字列の代わりにfileameとして使用できます。

PHPで例えば1はどうなる:

// 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

関連する問題