2016-07-25 3 views
-2

に以下のコードを修正するアドバイスは、ファイルを選択した時点でのjQuery

<input id="chunked_upload" type="file" name="the_file"> 

は、さまざまな部分でのファイルのアップロードを処理する次のコードは、自動的に

  var md5 = "", 
      csrf = $("input[name='csrfmiddlewaretoken']")[0].value, 
      form_data = [{"name": "csrfmiddlewaretoken", "value": csrf}]; 
     function calculate_md5(file, chunk_size) { 
      var slice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, 
       chunks = chunks = Math.ceil(file.size/chunk_size), 
       current_chunk = 0, 
       spark = new SparkMD5.ArrayBuffer(); 
      function onload(e) { 
      spark.append(e.target.result); // append chunk 
      current_chunk++; 
      if (current_chunk < chunks) { 
       read_next_chunk(); 
      } else { 
       md5 = spark.end(); 
      } 
      }; 
      function read_next_chunk() { 
      var reader = new FileReader(); 
      reader.onload = onload; 
      var start = current_chunk * chunk_size, 
       end = Math.min(start + chunk_size, file.size); 
      reader.readAsArrayBuffer(slice.call(file, start, end)); 
      }; 
      read_next_chunk(); 
     } 
     $("#chunked_upload").fileupload({ 
      url: "{% url 'api_chunked_upload' %}", 
      dataType: "json", 
      maxChunkSize: 100000, // Chunks of 100 kB 
      formData: form_data, 
      add: function(e, data) { // Called before starting upload 
      $("#messages").empty(); 
      // If this is the second file you're uploading we need to remove the 
      // old upload_id and just keep the csrftoken (which is always first). 
      form_data.splice(1); 
      calculate_md5(data.files[0], 100000); // Again, chunks of 100 kB 
      data.submit(); 
      }, 
      chunkdone: function (e, data) { // Called after uploading each chunk 
      if (form_data.length < 2) { 
       form_data.push(
       {"name": "upload_id", "value": data.result.upload_id} 
      ); 
      } 
      $("#messages").append($('<p>').text(JSON.stringify(data.result))); 
      var progress = parseInt(data.loaded/data.total * 100.0, 10); 
      /*$("#progress").text(Array(progress).join("=") + "> " + progress + "%");*/ 
      $('#progress .progress-bar').css('width',progress + '%'); 
      $('#progress .progress-bar').css('aria-valuenow',progress + '%'); 
      }, 
      done: function (e, data) { // Called when the file has completely uploaded 
      $.ajax({ 
       type: "POST", 
       url: "{% url 'api_chunked_upload_complete' %}", 
       data: { 
       csrfmiddlewaretoken: csrf, 
       upload_id: data.result.upload_id, 
       md5: md5 
       }, 
       dataType: "json", 
       success: function(data) { 
       $("#messages").append($('<p>').text(JSON.stringify(data))); 

       } 
      }); 
      }, 
     }); 

を実行しますが、私はしたくありませんコードは自動的に実行されます。あなただけ

<button id="enviar">Saludar</button> 

誰かが助言し、[次へ]ボタンをクリックするまで、私はそれが

+0

あなたの**特定の問題に関連しているタイトルを選択してください**。詳細については、[良い質問をするにはどうすればいいですか?](https://stackoverflow.com/help/how-to-ask)を参照してください。 – paolo

答えて

0
  • button#enviarのクリックリスナを追加する方法について説明します。

その後、

  • 移動fileuploadコール内部 このクリックイベントのコールバック:

    $('button#enviar').click(function(){ 
    
          $("#chunked_upload").fileupload(
    
           //.... 
    
    
        }) 
    

全体のコードは次のようになります。そして、

  var md5 = "", 
      csrf = $("input[name='csrfmiddlewaretoken']")[0].value, 
      form_data = [{"name": "csrfmiddlewaretoken", "value": csrf}]; 
     function calculate_md5(file, chunk_size) { 
      var slice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, 
       chunks = chunks = Math.ceil(file.size/chunk_size), 
       current_chunk = 0, 
       spark = new SparkMD5.ArrayBuffer(); 
      function onload(e) { 
      spark.append(e.target.result); // append chunk 
      current_chunk++; 
      if (current_chunk < chunks) { 
       read_next_chunk(); 
      } else { 
       md5 = spark.end(); 
      } 
      }; 
      function read_next_chunk() { 
      var reader = new FileReader(); 
      reader.onload = onload; 
      var start = current_chunk * chunk_size, 
       end = Math.min(start + chunk_size, file.size); 
      reader.readAsArrayBuffer(slice.call(file, start, end)); 
      }; 
      read_next_chunk(); 
     } 
     $('#enviar').click(function(){ 

       uploadFile(); 
      }); 

uploadFileは次のとおりです。

function uploadFile(){ 
     $("#chunked_upload").fileupload({ 
       url: "{% url 'api_chunked_upload' %}", 
       dataType: "json", 
       maxChunkSize: 100000, // Chunks of 100 kB 
       formData: form_data, 
       add: function(e, data) { // Called before starting upload 
       $("#messages").empty(); 
       // If this is the second file you're uploading we need to remove the 
       // old upload_id and just keep the csrftoken (which is always first). 
       form_data.splice(1); 
       calculate_md5(data.files[0], 100000); // Again, chunks of 100 kB 
       data.submit(); 
       }, 
       chunkdone: function (e, data) { // Called after uploading each chunk 
       if (form_data.length < 2) { 
        form_data.push(
        {"name": "upload_id", "value": data.result.upload_id} 
       ); 
       } 
       $("#messages").append($('<p>').text(JSON.stringify(data.result))); 
       var progress = parseInt(data.loaded/data.total * 100.0, 10); 
       /*$("#progress").text(Array(progress).join("=") + "> " + progress + "%");*/ 
       $('#progress .progress-bar').css('width',progress + '%'); 
       $('#progress .progress-bar').css('aria-valuenow',progress + '%'); 
       }, 
       done: function (e, data) { // Called when the file has completely uploaded 
       $.ajax({ 
        type: "POST", 
        url: "{% url 'api_chunked_upload_complete' %}", 
        data: { 
        csrfmiddlewaretoken: csrf, 
        upload_id: data.result.upload_id, 
        md5: md5 
        }, 
        dataType: "json", 
        success: function(data) { 
        $("#messages").append($('<p>').text(JSON.stringify(data))); 

        } 
       }); 
       }, 
      }); 


} 
+0

私はファイルを選択してボタンをクリックし、コードをejecupeにしたいと思います。しかし、あなたが私に言ったようにそれをするには、まずここで与えてからファイルを選択してプログラムを実行しなければなりません。どのように私はそれを修正することができますか? –

関連する問題