[我有一个Nativescript Angular应用程序,它以Blob Uint8Array格式从Rails服务器下载pdf,当我尝试保存它时,出现以下错误;
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
注意:我尝试将一些基本文本写入文件,并且此方法有效。因此,它必须与“数据”的格式有关。
更新:调试器中“数据”的结果
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的Ruby on Rails代码。在我的实际应用程序中,它是生成的pdf,而不是文件。发送工作相同。
def index
respond_to do |format|
format.html
format.pdf do
send_file("#{Rails.root}/public/test.pdf")
end
end
end
我认为这是由于{N}中的Blob的Polyfills。您可以将Blob对象转换为Byte数组。
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;
});