鸿蒙文件上传-从前端到后端详解,对比jq请求和鸿蒙arkts请求区别,对比new FormData()和鸿蒙arktsrequest.uploadFile
需要权限:ohos.permission.INTERNET
1.nodejs自定义书写上传后端接口
传输过来的数据放在files?.image下
router.post('/upload',(req, res) => { var form = new multiparty.Form(); form.uploadDir='public/images/uploads'; //上传图片保存的地址(目录必须存在) form.parse(req, function(err, fields, files) {// 1、fields:获取表单的数据 2、files:图片上传成功返回的信息 console.log(files,fields) const fileurl = Date.now()+ files?.image[0]?.originalFilename; let newPath = form.uploadDir + '/' +fileurl fs.renameSync(files.image[0].path,newPath); let imgUrl = myUrl.myUrl+'/images/uploads/' +fileurl res.send({ code: '200', message: '数据上传成功', data:imgUrl }) }) })
2,在jq中请求(new FormData())
var fileImg; //读取图片 function fileUpload(_this) { var fileReader = new FileReader();//创建文件读取对象 fileImg = _this.files[0];//获取file组件中的文件 } //上传图片 function uploadPictures() { var formData = new FormData(); //图片 if (fileImg != null) { formData.append("image", fileImg); } $.ajax({ url: 'http://localhost:3333/api/img/upload', type: 'post', data: formData, processData: false, contentType: false, success: function (res) { // var res = JSON.parse(res); console.log('upload success', res); // $('.img').attr('src', res.path); debugger }, error: function (err) { console.log('upload error', err); console.log(err); } }); }
3.对比在鸿蒙arkts请求如下 -试用相册的场景
参考官网
1.假设数据来源于相册,以下是获取相册的数据,photoSelectResult.photoUris[0]是一个目录的路径, 但是不是context.cacheDir的路径,
试用下载必须要在cacheDir目录下面
async selectPhoto() { try { let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); PhotoSelectOptions.maxSelectNumber = 1; PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; let textInfo: photoAccessHelper.TextContextInfo = { text: '人像' }; let recommendationOptions: photoAccessHelper.RecommendationOptions = { textContextInfo: textInfo }; PhotoSelectOptions.recommendationOptions = recommendationOptions; // 将推荐参数赋值给 photoSelectOptions.recommendationOptions let photoPicker = new photoAccessHelper.PhotoViewPicker(); let photoSelectResult = await photoPicker.select(PhotoSelectOptions) if (photoSelectResult.photoUris.length == 1) { this.uploadFile(photoSelectResult.photoUris[0]) } } catch (error) { let err: BusinessError = error as BusinessError; } }
2.因为必须是cacheDir目录,从相册拿到的的图片就复制一份存在cacheDir
// 提取文件扩展名的函数 getFileExtension(fileName: string): string { const lastDotIndex = fileName.lastIndexOf('.'); if (lastDotIndex !== -1 && lastDotIndex3。知道了cacheDir目录就组装参数
let files: Array = [ //uri前缀internal://cache 对应cacheDir目录 { filename: names, name: 'image', // 必须是image,因为接口拿的数据就是这个的第0项 uri: 'internal://cache/' + names, type: extensionName } ] let data: Array = [{ name: 'name', value: 'value' }]; let uploadConfig: request.UploadConfig = { url: 'http://xxxxxxxxxxxxxxxx/api/img/upload', header: { // 根据项目添加 }, method: 'POST', files: files, data: data, } // 将本地应用文件上传至网络服务器 try { request.uploadFile(context, uploadConfig) .then((uploadTask: request.UploadTask) => { let headerCallback = (value: object) => { let str = JSON.stringify(value) console.log("http:success:", JSON.stringify(rstr )) }; uploadTask.on('headerReceive', headerCallback); }) .catch((err: BusinessError) => { console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); }) } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`); }缓存目录在cacheDir ="/data/app/el2/100/base/{找到自己的包名}/haps/entry/cache”
原理总结
使用相册拿到的路径,复制一份放在缓存路径下, request.uploadFile(只能读缓存的路径。
通过 uploadTask.on(‘headerReceive’, headerCallback);拿到后端给我们的响应数据code
官网也还提供了request.agent,可查看文档使用
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/app-file-upload-download#%E4%B8%8A%E4%BC%A0%E5%BA%94%E7%94%A8%E6%96%87%E4%BB%B6
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。