鸿蒙开发(NEXT/API 12)【上传下载文件】远场通信场景
场景介绍
本协议栈框架支持将文件上传到服务器或者从服务器下载文件。
开发步骤
- 导包。
import { rcp } from '@kit.RemoteCommunicationKit';
import {fileIo} from '@kit.CoreFileKit';
- 下载文件。
let SESSION_CONFIG: rcp.SessionConfiguration = {// 此处请根据业务设置合适的Configuration
}
let DOWNLOAD_TO_PATH = ""; // 请根据自身业务选择合适的路径
// 1、下载到文件
const session = rcp.createSession(SESSION_CONFIG);
const respMemory = await session.get("https//www.example.com");
const dataMemory = respMemory.toString();
if (fileIo.accessSync(DOWNLOAD_TO_PATH)) {fileIo.unlinkSync(DOWNLOAD_TO_PATH);
}
const resp = await session.downloadToFile("https//www.example.com", {kind: 'file',file: DOWNLOAD_TO_PATH
});
session.close();
// 2、下载到目录
const session = rcp.createSession(SESSION_CONFIG);
const respMemory = await session.get("https//www.example.com");
const dataMemory = respMemory.toString();
const downloadedPath = "FILES_DIR" + "DOWNLOAD_FOLDER_SUB_PATH"; // 请根据自身业务选择合适的路径
if (fileIo.accessSync(downloadedPath)) {fileIo.unlinkSync(downloadedPath);
}
const resp = await session.downloadToFile("https//www.example.com", {kind: 'folder',path: "FILES_DIR" // 请根据自身业务选择合适的路径
});
session.close();
// 3、下载到文件描述符
const session = rcp.createSession(SESSION_CONFIG);
const respMemory = await session.get("https//www.example.com");
const dataMemory = respMemory.toString();
if (fileIo.accessSync(DOWNLOAD_TO_PATH)) {fileIo.unlinkSync(DOWNLOAD_TO_PATH);
}
const file = fileIo.openSync(DOWNLOAD_TO_PATH, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY);
const resp = await session.downloadToFile("https//www.example.com", {kind: 'file',file: file.fd
});
fileIo.closeSync(file.fd);
session.close();
// 4、下载到文件对象
const session = rcp.createSession(SESSION_CONFIG);
const respMemory = await session.get("https//www.example.com");
const dataMemory = respMemory.toString();
if (fileIo.accessSync(DOWNLOAD_TO_PATH)) {fileIo.unlinkSync(DOWNLOAD_TO_PATH);
}
const file = fileIo.openSync(DOWNLOAD_TO_PATH, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE_ONLY);
const resp = await session.downloadToFile("https//www.example.com", {kind: 'file',file: file
});
fileIo.closeSync(file.fd);
session.close();
- 将文件上传到指定路径。
let SESSION_CONFIG: rcp.SessionConfiguration = {// 此处请根据业务设置合适的Configuration
}
// 1、传入一个文件路径
const session = rcp.createSession(SESSION_CONFIG);
const resp = await session.uploadFromFile("https://www.example.com", new rcp.UploadFromFile("UPLOAD_FILE")); // 请根据自身业务填写
const json = resp.toJSON();
session.close();
// 2、传入一个文件描述符
const session = rcp.createSession(SESSION_CONFIG);
const file = fileIo.openSync("UPLOAD_FILE", fileIo.OpenMode.READ_ONLY); // 请根据自身业务填写UPLOAD_FILE
const resp = await session.uploadFromFile("https://www.example.com", new rcp.UploadFromFile(file.fd));
const json = resp.toJSON();
fileIo.closeSync(file.fd);
session.close();
// 3、传入一个文件对象
const session = rcp.createSession(SESSION_CONFIG);
const file = fileIo.openSync("UPLOAD_FILE", fileIo.OpenMode.READ_ONLY); // 请根军自身业务定义UPLOAD_FILE
const resp = await session.uploadFromFile("https://www.example.com", new rcp.UploadFromFile(file));
const json = resp.toJSON();
fileIo.closeSync(file.fd);
session.close();
// 4、使用自定义文件对象
class FdReadFile {readonly fd: number;constructor(fd: number) {this.fd = fd;}async read(buffer: ArrayBuffer): Promise<number> {return fileIo.read(this.fd, buffer);}
}
const session = rcp.createSession(SESSION_CONFIG);
const file = fileIo.openSync("UPLOAD_FILE", fileIo.OpenMode.READ_ONLY);
const resp = await session.uploadFromFile("https://www.example.com", new rcp.UploadFromFile(new FdReadFile(file.fd)));
const json = resp.toJSON();
fileIo.closeSync(file.fd);
session.close();
// 5、使用自定义文件流
class FdReadStream {readonly file: FdReadFile;constructor(fd: number) {this.file = new FdReadFile(fd);}async read(buffer: ArrayBuffer): Promise<number> {return this.file.read(buffer);}
}
const session = rcp.createSession(SESSION_CONFIG);
const file = fileIo.openSync("UPLOAD_FILE", fileIo.OpenMode.READ_ONLY); // 请根据自身业务定义UPLOAD_FILE
const resp = await session.uploadFromFile("https://www.example.com", new rcp.UploadFromFile(new FdReadStream(file.fd)));
const json = resp.toJSON();
fileIo.closeSync(file.fd);session.close();
session.close();
// 6、使用自定义文件流
class FdSyncReadStream {readonly file: FdReadFile;constructor(fd: number) {this.file = new FdReadFile(fd);}async read(buffer: ArrayBuffer): Promise<number> {return this.file.read(buffer);}
}
const session = rcp.createSession(SESSION_CONFIG);
const file = fileIo.openSync("UPLOAD_FILE", fileIo.OpenMode.READ_ONLY);
const resp = await session.uploadFromStream("https:www.example.com", new rcp.UploadFromStream(new FdSyncReadStream(file.fd)));
const json = resp.toJSON();
fileIo.closeSync(file.fd);
最后呢
很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。
- 《鸿蒙 (OpenHarmony)开发学习视频》
- 《鸿蒙生态应用开发V2.0白皮书》
- 《鸿蒙 (OpenHarmony)开发基础到实战手册》
- OpenHarmony北向、南向开发环境搭建
- 《鸿蒙开发基础》
- 《鸿蒙开发进阶》
- 《鸿蒙开发实战》
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿