2

I have a piece of AS code that must be translated to javascript. Most of the things work okay now but only uploading (in chunks) fail. I get a message back from the server "Different file size". I think the way javascript post raw data differs from AS3's UrlRequest with FormData.

For example:

  • AS3: urlRequest.data = [ByteArray]
  • JS : Formdata.append('data', [FileBlob] )

Does this have the same result?

The AS3 code is:

 private function sendData() : void
        {
            this._filePieceReadyToUpload = false;
            this._bytesToSend = new ByteArray();
            this._fileStream.readBytes(this._bytesToSend, 0, this._fileStream.bytesAvailable);
            this._currentOperation = this.OP_DATA;
            this._urlRequest = new URLRequest(_uploadScriptPath + "?op=" + this.OP_DATA + "&secure=" + _loginSecureHash + "&user_id=" + _userId + "&crc=" + this._headerCRC + "&size=" + this._bytesToSend.length + "&position=" + this._piecePosition + "&md5=" + this._fileCurrent.$md5 + "&api_id=" + _apiID);
            this._urlRequest.method = URLRequestMethod.POST;
            this._urlRequest.data = this._bytesToSend;
            this.startURLRequstLoadTimer();
            return;
        }

Translated into this (javascript):

  o.uploadFileBlob = function( blobFile, iBlobPos, sCrc ) 
  {
    var fd = new FormData();
        fd.append( 'data', blobFile ),
        xhr = new XMLHttpRequest(),
        sGetVars = 'op=data'+
                   '&secure='+o.apik_secureHash+
                   '&user_id='+o.apik_userId+
                   '&crc='+sCrc+
                   '&size='+blobFile.size+
                   '&position='+iBlobPos+
                   '&md5='+
                   '&api_id=4';

        xhr.upload.addEventListener("progress", function(e) { $d('progress'); }, false);
        xhr.addEventListener("load" , function(e) { $d('upload complete'); }, false);
        xhr.addEventListener("error", function(e) { $d('failed '+xhr.status+': '+xhr.statusText+' : '+xhr.responseText); 
                                                       //alert( xhr.getAllResponseHeaders().toLowerCase() ); 
                                                       }, false);
        xhr.addEventListener("abort", function(e) { $d('upload abort'); }, false);
        xhr.open('POST', o.apik_address+'?'+sGetVars, true );

        xhr.onload = function(e) { $d('xhr loaded'); };
        return xhr.send(fd);
  }

Can explain somebody to me what the difference is between the AS and JS version. Is a POST field 'data' in javascript the same as urlRequest.data in AS? What am I doing wrong?

Codebeat
  • 6,501
  • 6
  • 57
  • 99

0 Answers0