首页
社区
课程
招聘
[求助]请问这段JS代码什么意思?
发表于: 2013-9-7 20:06 4644

[求助]请问这段JS代码什么意思?

2013-9-7 20:06
4644
function uploadFile(filename, file, sha256) {

  /* send a GET request first to ask if the file
     already exists and get the upload URL */

  var data = {};

  if (sha256)
      data = {'sha256': sha256};

  var upload_url = $('#frm-file').attr('action');

  $.ajax({
    type: 'GET',
    async: true,
    url: upload_url,
    dataType: 'json',
    data: data,
    context: {'filename': filename},
    cache: false,
    success: function(response){

      if (response.file_exists) {
        $('#btn-file-reanalyse').attr(
          'href', response.reanalyse_url + '&filename=' + this.filename);
        $('#btn-file-view-last-analysis').attr(
          'href', response.last_analysis_url);
        $('#dlg-upload-progress').modal('hide');
        $('div#dlg-file-analysis-confirmation span#last-analysis-date').html(
          response.last_analysis_date);
        $('div#dlg-file-analysis-confirmation span#detection-ratio').html(
          '' + response.detection_ratio[0] + '/' + response.detection_ratio[1]);
        $('#dlg-file-analysis-confirmation').modal('show');
      }
      else {
        /* if browser have FormData support send the file via XMLHttpRequest
           with upload progress bar, else send it the standard way */

        if (file && window.FormData) {

          var fd = new FormData();

          fd.append('file', file);
          fd.append('ajax','true');

          /* Due to a bug in AppEngine we have to send the IP of the user as a
             param in this post. The server is sending us the IP it saw in the
             GET request, so we can send it back in the POST. This workaround
             should be removed when the issue is solved. See:
             (http://code.google.com/p/googleappengine/issues/detail?id=5175) */

          fd.append('remote_addr', response.remote_addr);

          if (sha256)
            fd.append('sha256', sha256);
          if (selectedFile.lastModifiedDate != undefined)
            fd.append(
                'last_modified', selectedFile.lastModifiedDate.toISOString());

          currentUpload = new XMLHttpRequest();

          currentUpload.upload.addEventListener(
            'progress',
            uploadProgress,
            false);
          currentUpload.addEventListener('load', uploadComplete, false);
          currentUpload.addEventListener('error', uploadFailed, false);
          currentUpload.open('POST', response.upload_url);
          currentUpload.send(fd);

        } else {

          $('#frm-file').attr('action', response.upload_url);
          $('#frm-file').submit();

          /*  in IE 7 animated GIFs freeze immediately after submit, we need
              this hack to reload the GIF and make the animation work during
              the file upload */

          $('#gif-upload-progress-bar span').html(
            '<img style="display:block" src="/static/img/bar.gif">');
        }
      }
    }
  }); // $.ajax()
}


function canUserWorker() {

  if (window.FileReader && window.Worker) {
    var major_version = parseInt(jQuery.browser.version, 10);

    if (jQuery.browser.opera)
      return false;

    if (jQuery.browser.mozilla && major_version >= 8)
      return true;

    if (jQuery.browser.webkit && major_version >= 535)
      return true;
  }

  return false;
}

function scanFile(evt) {

  if (!selectedFileName) {
    return;
  }

  if (selectedFile && selectedFile.size > 64*1024*1024) {
    $('#dlg-file-too-large').modal('show');
    return;
  }

  $('#dlg-upload-progress').modal('show');

  /* if browser has support for File API and Web Workers, calculate hash before
     upload in a separate thread. Opera supports both, but its postMessage
     implementation doesn't allow to pass a File object as a parameter, so we
     can't send the file to the worker  */

  if (canUserWorker()){

    $('#upload-progress-bar').hide();
    $('#hash-progress').css('width','0%');
    $('#hash-progress-bar').show();

    worker = new Worker('/static/js/sha256.js');
    worker.onmessage = function(e) {
      if (e.data.progress) {
        $('#hash-progress').css('width', e.data.progress + '%');
      }
      else {
        $('#hash-progress-bar').hide();
        $('#upload-progress').css('width','0%');
        $('#upload-progress-bar').show();

        uploadFile(selectedFileName, selectedFile, e.data.sha256);
      }
    };

    worker.postMessage({file: selectedFile});
  }
  else {
    $('#gif-upload-progress-bar').show();
    uploadFile(selectedFileName, null, null);
  }
}

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 81
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
目测流程是:
选择要上传的文件->检查浏览器支持(不支持IE,只支持火狐和谷歌?)->计算文件sha256-->拿着sha256询问服务器是否存在->如果不存在则上传文件
$.ajax用的是JQUERY库,网上有用法,请求大约是:
http://xxx?sha256=文件的sha256&rnd=随机数
2013-9-7 23:20
0
雪    币: 278
活跃值: (709)
能力值: ( LV15,RANK:520 )
在线值:
发帖
回帖
粉丝
3
非常感谢!!!!!!
2013-9-8 02:23
0
游客
登录 | 注册 方可回帖
返回
//