简体   繁体   English

是否可以使用健壮的javascript上传文件

[英]Is robust javascript-only upload of file possible

I want a robust way to upload a file. 我想要一种健壮的方式来上传文件。 That means that I want to be able to handle interruptions, error and pauses. 这意味着我希望能够处理中断,错误和暂停。

So my question is: Is something like the following possible using javascript only on the client . 所以我的问题是: 只有在客户端上使用javascript才能实现以下内容

If so I would like pointers to libraries, tutorials, books or implementations. 如果是这样,我想指向库,教程,书籍或实现。 If not I would like an explanation to why it's not possible. 如果不是,我想解释为什么不可能。

Scenario: 场景:

  • Open a large file 打开一个大文件
  • Split it into parts 将其拆分为多个部分

For each part I would like to 对于我想要的每个部分

  • Create checksum and append to data 创建校验和并附加到数据
  • Post data to server (the server would check if data uploaded correctly) 将数据发布到服务器(服务器会检查数据是否正确上传)
  • Check a web page on server to see if upload is ok 检查服务器上的网页以查看上传是否正常
  • If yes upload next part if no retry 如果是,则上传下一部分,如果没有重试

Assume all posts to server is accompanied by relevant meta data ( sessionid and whatnot ). 假设所有发布到服务器的帖子都附带相关的元数据( sessionid和whatnot )。

No. You can, through a certain amount of hackery, begin a file upload with AJAX, in which case you'll be able to tell when it's finished uploading. 你可以通过一定量的两轮牛车,皆以AJAX文件上传,在这种情况下,你就可以当它完成上传来告诉。 That's it. 而已。

JavaScript does not have any direct access to files on the visitor's computer for security reasons. 出于安全原因,JavaScript无法直接访问访问者计算机上的文件。 The most you'll be able to see from within your script is the filename. 您可以在脚本中看到的最多的是文件名。

Firefox 3.5 adds support for DOM progress event monitoring of XMLHttpRequest transfers which allow you to keep track of at least upload status as well as completion and cancellation of uploads. Firefox 3.5增加了对XMLHttpRequest传输的DOM进度事件监控的支持,使您可以跟踪至少上传状态以及上传的 完成取消

It's also possible to simulate progress tracking with iframes in clients that don't support this newer XMLHTTPRequest additions. 也可以在不支持这种新的XMLHTTPRequest添加的客户端中使用iframe模拟进度跟踪。

For an example of script that does just this, take a look at NoSWFUpload . 有关执行此操作的脚本示例,请查看NoSWFUpload I've been using it succesfully for about few months now. 我已经成功使用它几个月了。

It's possible in Firefox 3 to open a local file as chosen by a file upload field and read it into a JavaScript variable using the field's files array . 在Firefox 3中,可以打开由文件上载字段选择的本地文件,并使用字段的files数组将其读入JavaScript变量。 That would allow you to do your own chunking, hashing and sending by AJAX. 这将允许您通过AJAX进行自己的分块,散列和发送。

There is some talk of getting something like this standardised by W3, but for the immediate future no other browser supports this. 有一些关于通过W3标准化这样的东西的讨论,但是在不久的将来,没有其他浏览器支持这一点。

Yes. 是。 Please look at the following file - 请看以下文件 -

function Upload() {

  var self = this;

  this.btnUpload;

  this.frmUpload;

  this.inputFile;

  this.divUploadArea;

  this.upload = function(event, target) {
    event.stopPropagation();

    if (!$('.upload-button').length) {
      return false;
    }

    if (!$('.form').length) {
      return false;
    }  

    self.btnUpload = target;
    self.frmUpload = $(self.btnUpload).parents('form:first');
    self.inputFile = $(self.btnUpload).prev('.upload-input');
    self.divUploadArea = $(self.btnUpload).next('.uploaded-area');

    var target = $(self.frmUpload).attr('target');
    var action = $(self.frmUpload).attr('action');

    $(self.frmUpload).attr('target', 'upload_target'); //change the form's target to the iframe's id
    $(self.frmUpload).attr('action', '/trnUpload/upload'); //change the form's action to the upload iframe function page
    $(self.frmUpload).parent("div").prepend(self.iframe);

    $('#upload_target').load(function(event){

      if (!$("#upload_target").contents().find('.upload-success:first').length) {
        $('#upload_target').remove();
        return false;
      } else if($("#upload_target").contents().find('.upload-success:first') == 'false') {
        $('#upload_target').remove();
        return false;  
      }

      var fid = $("#upload_target").contents().find('.fid:first').html();
      var filename = $("#upload_target").contents().find('.filename:first').html();
      var filetype = $("#upload_target").contents().find('.filetype:first').html();
      var filesize = $("#upload_target").contents().find('.filesize:first').html();

      $(self.frmUpload).attr('target', target); //change the form's target to the iframe's id
      $(self.frmUpload).attr('action', action); //change the form's  
      $('#upload_target').remove();

      self.insertUploadLink(fid, filename, filetype, filesize);
    });

  };

  this.iframe = '' +
                'false

' + ''; this.insertUploadLink = function (fid, filename, filetype, filesize) { $('#upload-value').attr('value', fid); } } $(document).ready(event) { var myupload = new Upload(); myupload.upload(event, event.target); }

With also using PHP's APC to query the status of how much of the file has been uploaded, you can do a progress bar with a periodical updater (I would use jQuery, which the above class requires also). 通过使用PHP的APC查询已上传文件的状态,您可以使用定期更新程序进行进度条(我将使用jQuery,上述类也需要)。 You can use PHP to output both the periodical results, and the results of the upload in the iframe that is temporarily created. 您可以使用PHP输出定期结果,以及临时创建的iframe中的上载结果。

This is hackish. 这是hackish。 You will need to spend a lot of time to get it to work. 你需要花很多时间才能让它发挥作用。 You will need admin access to whatever server you want to run it on so you can install APC. 您需要管理员访问要运行它的任何服务器,以便安装APC。 You will also need to setup the HTML form to correspond to the js Upload class. 您还需要设置HTML表单以对应于js Upload类。 A reference on how to do this can be found here http://www.ultramegatech.com/blog/2008/12/creating-upload-progress-bar-php/ 有关如何执行此操作的参考资料,请访问http://www.ultramegatech.com/blog/2008/12/creating-upload-progress-bar-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM