|
3371 | 3371 | return builder; |
3372 | 3372 | } |
3373 | 3373 |
|
3374 | | - function send(e) { |
3375 | | - var xhr = new XMLHttpRequest(), |
3376 | | - boundary = '-backendless-multipart-form-boundary-' + getNow(), |
3377 | | - builder = getBuilder(this.fileName, e.target.result, boundary), |
3378 | | - badResponse = function(xhr) { |
3379 | | - var result = {}; |
3380 | | - try { |
3381 | | - result = JSON.parse(xhr.responseText); |
3382 | | - } catch (e) { |
3383 | | - result.message = xhr.responseText; |
3384 | | - } |
3385 | | - result.statusCode = xhr.status; |
3386 | | - return result; |
3387 | | - }; |
3388 | | - |
3389 | | - xhr.open("POST", this.uploadPath, true); |
3390 | | - xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary); |
3391 | | - xhr.setRequestHeader('application-id', Backendless.applicationId); |
3392 | | - xhr.setRequestHeader("secret-key", Backendless.secretKey); |
3393 | | - xhr.setRequestHeader("application-type", "JS"); |
3394 | | - |
3395 | | - if ((currentUser != null && currentUser["user-token"])) { |
3396 | | - xhr.setRequestHeader("user-token", currentUser["user-token"]); |
3397 | | - } else if (Backendless.LocalCache.exists("user-token")) { |
3398 | | - xhr.setRequestHeader("user-token", Backendless.LocalCache.get("user-token")); |
3399 | | - } |
3400 | | - |
3401 | | - if (UIState !== null) { |
3402 | | - xhr.setRequestHeader("uiState", UIState); |
3403 | | - } |
3404 | | - |
3405 | | - var asyncHandler = this.asyncHandler; |
| 3374 | + function sendData(options) { |
| 3375 | + var async = options.async; |
| 3376 | + var encoded = options.encoded; |
| 3377 | + var boundary = '-backendless-multipart-form-boundary-' + getNow(); |
| 3378 | + var xhr = new XMLHttpRequest(); |
3406 | 3379 |
|
3407 | | - if (asyncHandler) { |
3408 | | - xhr.onreadystatechange = function() { |
3409 | | - if (xhr.readyState == 4) { |
3410 | | - if (xhr.status >= 200 && xhr.status < 300) { |
3411 | | - asyncHandler.success(JSON.parse(xhr.responseText)); |
3412 | | - } else { |
3413 | | - asyncHandler.fault(JSON.parse(xhr.responseText)); |
3414 | | - } |
3415 | | - } |
3416 | | - }; |
3417 | | - } |
3418 | | - |
3419 | | - xhr.sendAsBinary(builder); |
| 3380 | + var badResponse = function (xhr) { |
| 3381 | + var result = {}; |
3420 | 3382 |
|
3421 | | - if (asyncHandler) { |
3422 | | - return xhr; |
3423 | | - } |
| 3383 | + try { |
| 3384 | + result = JSON.parse(xhr.responseText); |
| 3385 | + } catch (e) { |
| 3386 | + result.message = xhr.responseText; |
| 3387 | + } |
3424 | 3388 |
|
3425 | | - if (xhr.status >= 200 && xhr.status < 300) { |
3426 | | - return xhr.responseText ? JSON.parse(xhr.responseText) : true; |
3427 | | - } else { |
3428 | | - throw badResponse(xhr); |
3429 | | - } |
3430 | | - } |
| 3389 | + result.statusCode = xhr.status; |
3431 | 3390 |
|
3432 | | - function sendEncoded(e) { |
3433 | | - var xhr = new XMLHttpRequest(), |
3434 | | - boundary = '-backendless-multipart-form-boundary-' + getNow(), |
3435 | | - badResponse = function(xhr) { |
3436 | | - var result = {}; |
3437 | | - try { |
3438 | | - result = JSON.parse(xhr.responseText); |
3439 | | - } catch (e) { |
3440 | | - result.message = xhr.responseText; |
3441 | | - } |
3442 | | - result.statusCode = xhr.status; |
3443 | | - return result; |
3444 | | - }; |
| 3391 | + return result; |
| 3392 | + }; |
3445 | 3393 |
|
3446 | | - var asyncHandler = this.asyncHandler; |
| 3394 | + xhr.open(options.method, options.url, !!async); |
3447 | 3395 |
|
3448 | | - xhr.open("PUT", this.uploadPath, !!asyncHandler); |
3449 | | - xhr.setRequestHeader('Content-Type', 'text/plain'); |
3450 | 3396 | xhr.setRequestHeader('application-id', Backendless.applicationId); |
3451 | 3397 | xhr.setRequestHeader("secret-key", Backendless.secretKey); |
3452 | 3398 | xhr.setRequestHeader("application-type", "JS"); |
3453 | 3399 |
|
| 3400 | + if (encoded) { |
| 3401 | + xhr.setRequestHeader('Content-Type', 'text/plain'); |
| 3402 | + } else { |
| 3403 | + xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary); |
| 3404 | + } |
| 3405 | + |
3454 | 3406 | if (UIState !== null) { |
3455 | 3407 | xhr.setRequestHeader("uiState", UIState); |
3456 | 3408 | } |
3457 | 3409 |
|
3458 | | - if (asyncHandler) { |
| 3410 | + var userToken = currentUser && currentUser["user-token"] || Backendless.LocalCache.get("user-token"); |
| 3411 | + |
| 3412 | + if (userToken) { |
| 3413 | + xhr.setRequestHeader("user-token", userToken); |
| 3414 | + } |
| 3415 | + |
| 3416 | + if (async) { |
3459 | 3417 | xhr.onreadystatechange = function() { |
3460 | 3418 | if (xhr.readyState == 4) { |
3461 | 3419 | if (xhr.status >= 200 && xhr.status < 300) { |
3462 | | - asyncHandler.success(JSON.parse(xhr.responseText)); |
| 3420 | + async.success(JSON.parse(xhr.responseText)); |
3463 | 3421 | } else { |
3464 | | - asyncHandler.fault(JSON.parse(xhr.responseText)); |
| 3422 | + async.fault(JSON.parse(xhr.responseText)); |
3465 | 3423 | } |
3466 | 3424 | } |
3467 | 3425 | }; |
3468 | 3426 | } |
3469 | 3427 |
|
3470 | | - xhr.send(e.target.result.split(',')[1]); |
| 3428 | + if (encoded) { |
| 3429 | + xhr.send(options.data); |
| 3430 | + } else { |
| 3431 | + xhr.sendAsBinary(getBuilder(options.fileName, options.data, boundary)); |
| 3432 | + } |
3471 | 3433 |
|
3472 | | - if (asyncHandler) { |
| 3434 | + if (async) { |
3473 | 3435 | return xhr; |
3474 | 3436 | } |
3475 | 3437 |
|
|
3571 | 3533 |
|
3572 | 3534 | try { |
3573 | 3535 | var reader = new FileReader(); |
3574 | | - reader.fileName = encodeURIComponent(fileName).replace(/'/g, "%27").replace(/"/g, "%22"); |
3575 | | - reader.uploadPath = baseUrl; |
3576 | | - reader.onloadend = sendEncoded; |
3577 | | - |
3578 | | - if (async) { |
3579 | | - reader.asyncHandler = async; |
3580 | | - } |
| 3536 | + var fileName = encodeURIComponent(fileName).replace(/'/g, "%27").replace(/"/g, "%22"); |
| 3537 | + reader.fileName = fileName; |
| 3538 | + reader.onloadend = function (e) { |
| 3539 | + sendData({ |
| 3540 | + url: baseUrl, |
| 3541 | + data: e.target.result.split(',')[1], |
| 3542 | + fileName: fileName, |
| 3543 | + encoded: true, |
| 3544 | + async: async, |
| 3545 | + method: 'PUT' |
| 3546 | + }); |
| 3547 | + }; |
3581 | 3548 |
|
3582 | 3549 | reader.onerror = function(evn) { |
3583 | 3550 | async.fault(evn); |
|
3613 | 3580 | for (var i = 0, len = files.length; i < len; i++) { |
3614 | 3581 | try { |
3615 | 3582 | var reader = new FileReader(); |
3616 | | - reader.fileName = encodeURIComponent(files[i].name).replace(/'/g, "%27").replace(/"/g, "%22"); |
3617 | | - reader.uploadPath = baseUrl + reader.fileName + overwriting; |
3618 | | - reader.onloadend = send; |
3619 | | - reader.asyncHandler = async; |
| 3583 | + var fileName = encodeURIComponent(files[i].name).replace(/'/g, "%27").replace(/"/g, "%22"); |
| 3584 | + var url = baseUrl + fileName + overwriting; |
| 3585 | + |
| 3586 | + reader.fileName = fileName; |
| 3587 | + reader.onloadend = function (e) { |
| 3588 | + sendData({ |
| 3589 | + url: url, |
| 3590 | + data: e.target.result, |
| 3591 | + fileName: fileName, |
| 3592 | + async: async, |
| 3593 | + method: 'POST' |
| 3594 | + }); |
| 3595 | + }; |
| 3596 | + |
3620 | 3597 | reader.onerror = function(evn) { |
3621 | 3598 | async.fault(evn); |
3622 | 3599 | }; |
3623 | | - reader.readAsArrayBuffer(files[i]); |
| 3600 | + reader.readAsBinaryString(files[i]); |
3624 | 3601 |
|
3625 | 3602 | } catch (err) { |
3626 | 3603 | filesError++; |
|
0 commit comments