我有一个Nativescript角应用程序,从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注意:我试着将一些基本文本写到一个文件中,这是有效的。因此,它必须与“数据”的格式有关。
更新:调试器中“数据”的结果
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代码。在我的实际应用程序中,它是生成的pdf,而不是文件。不过,发送的效果是一样的。
def index
respond_to do |format|
format.html
format.pdf do
send_file("#{Rails.root}/public/test.pdf")
end
end
end发布于 2020-05-13 23:46:30
我想这应该是由于{N}中的Blob填充。可以将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;
});https://stackoverflow.com/questions/61742581
复制相似问题