myml / myml.github.io

myml的个人网站
https://myml.dev
3 stars 0 forks source link

qml下载二进制文件 #40

Open myml opened 1 year ago

myml commented 1 year ago

在 qml 中可以使用XMLHttpRequest发送网络请求并获取返回内容,XMLHttpRequest 有个 responseType 属性, QT 文档中是这样说的:

responseType : string Returns a String describing the content type of the last response received.

If the response type is "text" or an empty String, the response content is a UTF-16 encoded String. If the response type is "arraybuffer", it means that the response content is an ArrayBuffer containing binary data. If the response type is "json", the response content should be a JSON Document. If the response type is "document", it means that the response content is an XML Document, which can be safely read with the responseXML property.

就我理解是先获取 responseType 的值,然后再按不同的类型读取 response ,实际上 response 的值是空的,需要手动设置 responseType ,response 才会更改为对应的类型。

如果想使用XMLHttpRequest获取二进制数据,比如图片文件,需要设置 responseType = "arraybuffer" , response 的值才会是 ArrayBuffer 类型,该类型传递给 Qt 会自动转换为 QByteArray 类型,如果不设置 responseType,response 默认是string类型,传递给 Qt 时,使用 QByteArray 类型接收并不会报错,但数据是无效的无法使用。