GeezerVison is showing github.com — fix: support ArrayBuffer/Blob for form data · nativescript-community/https@54140de
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/https/request.android.ts +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -519,7 +519,19 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr |
519 | 519 | if (param.data instanceof okhttp3.RequestBody) { |
520 | 520 | builder.addFormDataPart(param.parameterName, param.fileName, param.data); |
521 | 521 | } else { |
522 | | - builder.addFormDataPart(param.parameterName, param.fileName, okhttp3.RequestBody.create(param.data, okhttp3.MediaType.parse(param.contentType))); |
| 522 | + let nData = param.data; |
| 523 | + if (param.data instanceof ArrayBuffer) { |
| 524 | + const typedArray = new Uint8Array(param.data); |
| 525 | + const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray)); |
| 526 | + nData = nativeBuffer.array(); |
| 527 | + } else if (param.data instanceof Blob) { |
| 528 | + // Stolen from core xhr, not sure if we should use InternalAccessor, but it provides fast access. |
| 529 | + // @ts-ignore |
| 530 | + const typedArray = new Uint8Array(Blob.InternalAccessor.getBuffer(param.data).buffer.slice(0) as ArrayBuffer); |
| 531 | + const nativeBuffer = java.nio.ByteBuffer.wrap(Array.from(typedArray)); |
| 532 | + nData = nativeBuffer.array(); |
| 533 | + } |
| 534 | + builder.addFormDataPart(param.parameterName, param.fileName, okhttp3.RequestBody.create(nData, okhttp3.MediaType.parse(param.contentType))); |
523 | 535 | } |
524 | 536 | } else { |
525 | 537 | if (typeof param.data === 'string') { |
|
Collapse file
Copy file name to clipboardExpand all lines: src/https/request.ios.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -464,7 +464,16 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr |
464 | 464 | let data = param.data; |
465 | 465 | if (typeof data === 'string') { |
466 | 466 | data = NSString.stringWithString(data).dataUsingEncoding(NSUTF8StringEncoding); |
| 467 | + } else if (data instanceof ArrayBuffer) { |
| 468 | + const buffer = new Uint8Array(data); |
| 469 | + data = NSData.dataWithData(buffer as any); |
| 470 | + } else if (data instanceof Blob) { |
| 471 | + // Stolen from core xhr, not sure if we should use InternalAccessor, but it provides fast access. |
| 472 | + // @ts-ignore |
| 473 | + const buffer = new Uint8Array(Blob.InternalAccessor.getBuffer(data).buffer.slice(0) as ArrayBuffer); |
| 474 | + data = NSData.dataWithData(buffer as any); |
467 | 475 | } |
| 476 | + |
468 | 477 | formData.appendPartWithFileDataNameFileNameMimeType(data, param.parameterName, param.fileName, param.contentType); |
469 | 478 | } |
470 | 479 | } else { |
|