nativescript 保存 blob 错误 Cannot convert object to [B at index 1
nativescript saving a blob error Cannot convert object to [B at index 1
我有一个 Nativescript Angular 应用程序,它从 Rails 服务器下载 PDF 作为 Blob Uint8Array 格式,当我尝试保存它时出现以下错误;
JS: /data/user/0/com.asset.management/files/result.pdf
JS: ERROR Error: Uncaught (in promise): Error: Cannot convert object to [B at index 1
JS: Error: Cannot convert object to [B at index 1
JS: at file: node_modules/@nativescript/core/file-system/file-system-access.android.js:215:0
JS: at new ZoneAwarePromise (file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)
JS: at FileSystemAccess.push.../node_modules/@nativescript/core/file-system/file-system-access.js.FileSystemAccess.writeAsync (file: node_modules/@nativescript/core/file-system/file-system-access.android.js:213:0)
JS: at file: node_modules/@nativescript/core/file-system/file-system.js:240:0
JS: at new ZoneAwarePromise (file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)
JS: at File.push.../node_modules/@nativescript/core/file-system/file-system.js.File.write (file: node_modules/@nativescript/core/file-system/file-system.js:231:0)
JS: at SafeSubscriber._next (file: src/app/assets/assets.component.ts:92:25)
JS: at SafeSubscriber.push.../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUn...
这是代码;
this.http.get(url, { responseType: 'blob' })
.subscribe(data => {
const documentsFolder = knownFolders.documents();
const destination = path.join(documentsFolder.path, 'result.pdf');
const file = File.fromPath(destination);
file.write(data);
this.pdfUrl = file.path;
});
我正在尝试保存文件,以便我可以使用 nativescript-pdf-view 插件查看它
更新:检查文件系统
我访问了我的模拟器并找到了它应该写入文件的目录,它就在那里,但它的大小为零字节。
: adb root
: adb shell
: su
: cd /data/user/0/com.asset.management/files
: ls -al
Results
: -rw------- 1 u0_a78 u0_a78 0 2020-05-12 14:20 result.pdf
注意:我尝试将一些基本文本写入文件,这成功了。所以它一定与 'data' 所在的格式有关。
更新:调试器'data'的结果
Blob {_buffer: Uint8Array(23251), _size: 23251, _type: "application/pdf", Symbol(Symbol.toStringTag): "Blob"}
size: 23251
type: "application/pdf"
Symbol(Symbol.toStringTag): "Blob"
_buffer: Uint8Array(23251) [37, 80, 68, 70, 45, 49, 46, 52, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 60, 10, 47, 84, 105, 116, 108, 101, 32, 40, 254, 255, 41, 10, 47, 67, 114, 101, 97, 116, 111, 114, 32, 40, 254, 255, 0, 119, 0, 107, 0, 104, 0, 116, 0, 109, 0, 108, 0, 116, 0, 111, 0, 112, 0, 100, 0, 102, 0, 32, 0, 48, 0, 46, 0, 49, 0, 50, 0, 46, 0, 53, 41, 10, 47, 80, 114, 111, 100, 117, 99, 101, 114, 32, 40, 254, 255, 0, 81, 0, 116, 0, …]
_size: 23251
_type: "application/pdf"
__proto__: Object
更新:创建了游乐场示例
https://play.nativescript.org/?template=play-ng&id=gA70GM&v=2
更新:后台发送
这是我在示例中用于发送 test.pdf 的 Rails 代码上的 Ruby。在我的实际应用程序中,它是生成的 pdf 而不是文件。不过发送的工作原理是一样的。
def index
respond_to do |format|
format.html
format.pdf do
send_file("#{Rails.root}/public/test.pdf")
end
end
end
我认为这是由于 {N} 中 Blob 的 Polyfill。您可以将 Blob 对象转换为字节数组。
this.http.get(url, { responseType: 'blob' })
.subscribe(data => {
const documentsFolder = knownFolders.documents();
const destination = path.join(documentsFolder.path, 'result.pdf');
const file = File.fromPath(destination);
const blob = (Array as any).create("byte", data.size);
for (let i = 0; i < blob.length; i++) {
blob[i] = (data as any)._buffer[i];
}
file.write(blob);
this.pdfUrl = file.path;
});
我有一个 Nativescript Angular 应用程序,它从 Rails 服务器下载 PDF 作为 Blob Uint8Array 格式,当我尝试保存它时出现以下错误;
JS: /data/user/0/com.asset.management/files/result.pdf
JS: ERROR Error: Uncaught (in promise): Error: Cannot convert object to [B at index 1
JS: Error: Cannot convert object to [B at index 1
JS: at file: node_modules/@nativescript/core/file-system/file-system-access.android.js:215:0
JS: at new ZoneAwarePromise (file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)
JS: at FileSystemAccess.push.../node_modules/@nativescript/core/file-system/file-system-access.js.FileSystemAccess.writeAsync (file: node_modules/@nativescript/core/file-system/file-system-access.android.js:213:0)
JS: at file: node_modules/@nativescript/core/file-system/file-system.js:240:0
JS: at new ZoneAwarePromise (file: node_modules/nativescript-angular/zone-js/dist/zone-nativescript.js:902:0)
JS: at File.push.../node_modules/@nativescript/core/file-system/file-system.js.File.write (file: node_modules/@nativescript/core/file-system/file-system.js:231:0)
JS: at SafeSubscriber._next (file: src/app/assets/assets.component.ts:92:25)
JS: at SafeSubscriber.push.../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUn...
这是代码;
this.http.get(url, { responseType: 'blob' })
.subscribe(data => {
const documentsFolder = knownFolders.documents();
const destination = path.join(documentsFolder.path, 'result.pdf');
const file = File.fromPath(destination);
file.write(data);
this.pdfUrl = file.path;
});
我正在尝试保存文件,以便我可以使用 nativescript-pdf-view 插件查看它
更新:检查文件系统
我访问了我的模拟器并找到了它应该写入文件的目录,它就在那里,但它的大小为零字节。
: adb root
: adb shell
: su
: cd /data/user/0/com.asset.management/files
: ls -al
Results
: -rw------- 1 u0_a78 u0_a78 0 2020-05-12 14:20 result.pdf
注意:我尝试将一些基本文本写入文件,这成功了。所以它一定与 'data' 所在的格式有关。
更新:调试器'data'的结果
Blob {_buffer: Uint8Array(23251), _size: 23251, _type: "application/pdf", Symbol(Symbol.toStringTag): "Blob"}
size: 23251
type: "application/pdf"
Symbol(Symbol.toStringTag): "Blob"
_buffer: Uint8Array(23251) [37, 80, 68, 70, 45, 49, 46, 52, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 60, 10, 47, 84, 105, 116, 108, 101, 32, 40, 254, 255, 41, 10, 47, 67, 114, 101, 97, 116, 111, 114, 32, 40, 254, 255, 0, 119, 0, 107, 0, 104, 0, 116, 0, 109, 0, 108, 0, 116, 0, 111, 0, 112, 0, 100, 0, 102, 0, 32, 0, 48, 0, 46, 0, 49, 0, 50, 0, 46, 0, 53, 41, 10, 47, 80, 114, 111, 100, 117, 99, 101, 114, 32, 40, 254, 255, 0, 81, 0, 116, 0, …]
_size: 23251
_type: "application/pdf"
__proto__: Object
更新:创建了游乐场示例
https://play.nativescript.org/?template=play-ng&id=gA70GM&v=2
更新:后台发送
这是我在示例中用于发送 test.pdf 的 Rails 代码上的 Ruby。在我的实际应用程序中,它是生成的 pdf 而不是文件。不过发送的工作原理是一样的。
def index
respond_to do |format|
format.html
format.pdf do
send_file("#{Rails.root}/public/test.pdf")
end
end
end
我认为这是由于 {N} 中 Blob 的 Polyfill。您可以将 Blob 对象转换为字节数组。
this.http.get(url, { responseType: 'blob' })
.subscribe(data => {
const documentsFolder = knownFolders.documents();
const destination = path.join(documentsFolder.path, 'result.pdf');
const file = File.fromPath(destination);
const blob = (Array as any).create("byte", data.size);
for (let i = 0; i < blob.length; i++) {
blob[i] = (data as any)._buffer[i];
}
file.write(blob);
this.pdfUrl = file.path;
});