更新记录
1.0.0(2026-02-03)
首版发布-安卓实现ftp文件上传
平台兼容性
uni-app x(4.85)
| Chrome | Safari | Android | Android插件版本 | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|---|
| - | - | 7.0 | 1.0.0 | - | - | - |
FTP上传插件 (cloudMaster-ftp)
插件介绍
这是一个用于 uni-app x 安卓平台的 FTP 文件上传插件,支持单个文件上传、批量上传、连接测试和远程目录创建功能。
功能特点
- ✅ 单个文件 FTP 上传
- ✅ 文件上传
- ✅ FTP 连接测试
平台支持
- uni-app x: Android 平台
安装方式
通过 HBuilderX 插件市场搜索 "cloudMaster-ftp" 进行安装,或直接将此插件目录复制到项目的 uni_modules 文件夹下。
使用说明
1. 导入插件
// 导入插件
import { FTPUploaderAndroid } from "@/uni_modules/cloudMaster-ftp"
2. 单个文件上传
this.ftpUploader = new FTPUploaderAndroid()
//选择文件
selectFile() : void {
uni.chooseFile({
count: 1,
type: 'all',
success: (res) => {
if (res.tempFiles.length > 0) {
const file = res.tempFiles[0]
this.selectedFile = {
path: file.path,
name: file.name,
size: file.size
}
this.status = `已选择: ${file.name} (${this.formatFileSize(file.size)})`
}
},
complete: (res) => {
console.log(res)
}
})
},
// 连接FTP(Android需要)
const connected = await this.ftpUploader.connect(config.host, config.port as Int, config.username, config.password, config.timeout as Int)
if (connected == true) {
const remotePath = `/sd_card/${this.selectedFile.name}`
const path = this.selectedFile.path as string
// 上传文件
const uploaded = await this.ftpUploader.uploadFile(
path,
remotePath
)
if (uploaded) {
this.status = `文件上传成功: ${remotePath}`
this.progress = 100
this.$emit('success', remotePath)
} else {
this.status = "文件上传失败"
this.$emit('fail', remotePath)
}
} else {
this.status = "连接失败"
}
错误码说明
| 错误码 | 说明 |
|---|---|
| 9020001 | 参数错误 - 缺少必要参数或参数格式不正确 |
| 9020002 | 连接错误 - FTP服务器连接失败 |
| 9020003 | 认证错误 - 用户名或密码错误 |
| 9020004 | 文件错误 - 本地文件不存在或远程文件操作失败 |
| 9020005 | 未知错误 - 上传过程中发生意外错误 |
注意事项
- 确保应用具有文件读取权限,在 AndroidManifest.xml 中添加相关权限
- 远程文件路径必须包含文件名,如
/test.txt - 批量上传时,所有文件使用相同的 FTP 配置
- 本插件目前仅支持 Android 平台
使用示例
// uniapp组件使用示例
<template>
<uni-popup ref="upload">
<!-- #ifdef APP -->
<scroll-view direction="vertical" style="flex:1" class="container">
<!-- #endif -->
<button @click="selectFile" class="action-button">选择文件</button>
<button @click="uploadToFTP" class="action-button">上传</button>
<text v-if="status" class="status">{{ status }}</text>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</uni-popup>
</template>
<script>
import { FTPUploaderAndroid } from "@/uni_modules/cloudMaster-ftp"
interface SelectedFile {
path : string
name : string
size : number
}
type FTPConfig = {
host : string
port : number
username : string
password : string
timeout : number
}
export default {
data() {
return {
ftpUploader: new FTPUploaderAndroid(),
selectedFile: {
path: '',
name: '',
size: 0
} as UTSJSONObject,
status: "",
progress: 0
}
},
methods: {
// 打开弹窗
open() {
(this.$refs['upload'] as ComponentPublicInstance).$callMethod("open");
},
// 关闭弹窗
close() {
(this.$refs['upload'] as ComponentPublicInstance).$callMethod("close");
},
selectFile() : void {
uni.chooseFile({
count: 1,
type: 'all',
success: (res) => {
if (res.tempFiles.length > 0) {
const file = res.tempFiles[0]
this.selectedFile = {
path: file.path,
name: file.name,
size: file.size
}
this.status = `已选择: ${file.name} (${this.formatFileSize(file.size)})`
}
},
complete: (res) => {
console.log(res)
}
})
},
async uploadToFTP() : Promise<void> {
if (this.selectedFile.path == null) {
this.status = "请先选择文件"
return
}
const config : FTPConfig = {
host: "192.168.10.123",
port: 21,
username: "molink",
password: "molinkadmin",
timeout: 3000
}
try {
this.status = "正在连接到FTP服务器..."
// 连接FTP(Android需要)
const connected = await this.ftpUploader.connect(config.host, config.port as Int, config.username, config.password, config.timeout as Int)
if (connected == true) {
const remotePath = `/sd_card/${this.selectedFile.name}`
const path = this.selectedFile.path as string
// 上传文件
const uploaded = await this.ftpUploader.uploadFile(
path,
remotePath
)
if (uploaded) {
this.status = `文件上传成功: ${remotePath}`
this.progress = 100
this.$emit('success', remotePath)
} else {
this.status = "文件上传失败"
this.$emit('fail', remotePath)
}
} else {
this.status = "连接失败"
}
} catch (error) {
console.log('上传错误', error)
this.status = `上传错误: ${error}`
} finally {
// 断开连接(Android需要)
this.ftpUploader.disconnect()
}
},
formatFileSize(bytes : number) : string {
if (bytes === 0) return "0 B"
const k = 1024
const sizes = ["B", "KB", "MB", "GB"]
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]
}
},
onUnload() {
// 页面卸载时断开连接
this.ftpUploader.disconnect()
}
}
</script>
<style>
.container {
width: 200px;
padding: 20rpx;
background-color: #fff;
border-radius: 10px;
}
.status {
margin-top: 20rpx;
color: #333;
}
.action-button {
margin: 15px 0;
}
.progress {
margin-top: 10rpx;
color: #007AFF;
}
</style>
常见问题
- 权限问题:确保应用具有读取存储权限
- 连接超时:检查FTP服务器地址、端口是否正确,以及网络连接状态
- 认证失败:确认用户名和密码是否正确
- 文件不存在:验证本地文件路径是否正确,文件是否存在
更新日志
v1.0.0
- 初始版本发布
- 支持单个文件上传
- 支持批量文件上传
- 支持连接测试和目录创建
- 完整的错误处理机制

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 27
赞赏 0
下载 11202107
赞赏 1855
赞赏
京公网安备:11010802035340号