跳到主要内容

files.{binaryId}

获取文件内容

  • 请求路由: files.{binaryId}
  • 交互模型: Request Stream
  • 访问限制: 无

可使用对应的HTTP URL直接获取文件内容:
https://sit.aspiretop.com/v2/api/files/user:user1@aspiretop:avatar.png
获取对应缩略图:
https://sit.aspiretop.com/v2/api/files/user:user1@aspiretop:avatar.png?thumb=true
也可以参考下面代码在网页中下载文件:

文件下载示例
function fileMeta(binaryId) {
return RSocket.rr({ route: `files.${Util.escapeRoute(binaryId)}.meta` });
}
function getFile(binaryId) {
return RSocket.rs({ route: `files.${Util.escapeRoute(binaryId)}`, binary: true });
}

function download() {
const binaryId = 'user:user1@aspiretop:avatar.png';
setDownloading(true);
fileMeta(binaryId).then((meta) => {
getFile(binaryId).then((data) => {
const { mimeType: type, filename } = meta;
const blob = new Blob(data.map((buf) => Uint8Array.from(buf.data).buffer), { type });
const objectUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = objectUrl;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(link.href);
link.remove();
});
}).catch((e) => FAIL(e))
.finally(() => setDownloading(false));
}

<Button type="primary" onClick={() => download()} loading={downloading}>下载文件</Button>
使用下载的二进制内容展示图片
// 定义src状态
const [imgSrc, setImgSrc] = useState();

useEffect(() => {
(async () => {
// 获取图片数据
const data = await apiFile.download('user:user1@aspiretop:avatar.png');
// 将图片数据转换为Buffer
const buf = Buffer.concat(data.map((b) => Uint8Array.from(b.data)));
// 使用Buffer构造用于图片显示的url
setImgSrc(URL.createObjectURL(new Blob([buf], { type: 'image/png' })));
})();
}, []);

// 显示图片
<div>
{imgSrc && <img src={imgSrc} />}
</div>

请求​

RSocket.rs({
"route" : "files.user:user1@aspiretop:avatar.png",
"args" : {
"thumb" : "为true时返回缩略图内容"
}
});

请求参数​

名称类型必须说明
binaryIdString是文件标识
thumbBoolean否为true时返回缩略图内容

返回​

文件二进制内容

[文件二进制内容]