HarmonyOS web上传文件和图片

希望提供上传文件和图片的web开发demo。

HarmonyOS
2024-10-24 13:10:59
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

上传文件的demo如下:

import web_webview from '@ohos.web.webview';  
import picker from '@ohos.file.picker';  
import { BusinessError } from '@ohos.base';  
  
@Entry  
@Component  
struct UploadFile {  
  controller:web_webview.WebviewController = new web_webview.WebviewController();  
  
  build() {  
    Column() {  
      Web({ src: $rawfile('setAttrAndEvent/uploadFile/index.html'), controller: this.controller })  
        .onShowFileSelector((event) => {  
          const documentSelectOptions = new picker.DocumentSelectOptions();  
          let uri: string | null = null;  
          const documentViewPicker = new picker.DocumentViewPicker();  
          documentViewPicker.select(documentSelectOptions)  
            .then((documentSelectResult) => {  
              uri = documentSelectResult[0];  
              if (event) {  
                event.result.handleFileList([uri]);  
              }  
            }).catch((err: BusinessError) => {  
            console.error(`Invoke documentViewPicker.select failed, code is ${err.code}, message is ${err.message}`);  
          })  
          return true  
        })  
    }  
  }  
}

上传图片的demo如下:

import web_webview from '@ohos.web.webview';  
import picker from '@ohos.file.picker';  
import { BusinessError } from '@ohos.base';  
  
@Entry  
@Component  
struct UploadFile {  
  controller:web_webview.WebviewController = new web_webview.WebviewController();  
  @State uri: Array<string> | null = null;  
  build() {  
    Column(){  
      Text('选中的图片')  
      List(){  
        ForEach(this.uri,(item:string)=>{  
          ListItem(){  
            Row(){  
              Image(item)  
                .width('30vh')  
                .height('30vh')  
            }  
  
          }  
          .width('30vh')  
          .height('30vh')  
        })  
      }  
      .width('30vh')  
      .height('30vh')  
      Web({  
        src:$rawfile('setAttrAndEvent/uploadFile/index.html'),  
        controller:this.controller  
      })  
        .width('100%')  
        .height('100vh')  
        .backgroundColor('grey')  
        .onShowFileSelector((event)=>{  
          console.log('MyFileUploader onShowFileSelector invoked')  
          let PhotoSelectOptions = new picker.PhotoSelectOptions();  
          PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;  
          PhotoSelectOptions.maxSelectNumber = 5;  
  
          const photoPicker = new picker.PhotoViewPicker();  
          photoPicker .select(PhotoSelectOptions).then((photoSelectResult) => {  
            this.uri = photoSelectResult.photoUris;  
            console.info('photoPicker .select to file succeed and photoSelectResult is:' + JSON.stringify(photoSelectResult));  
            console.info('photoPicker .select to file succeed and uri is:' + this.uri);  
            if (event) {  
              event.result.handleFileList(this.uri);  
            }  
          }).catch((err: BusinessError) => {  
            console.error(`Invoke photoPicker .select failed, code is ${err.code}, message is ${err.message}`);  
          })  
          return true;  
        })  
    }  
  }  
}

index.html页面如下:

<!doctype html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport"  
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">  
    <meta http-equiv="X-UA-Compatible" content="ie=edge">  
    <title>Document</title>  
</head>  
<body>  
<div class="upload">  
    <!--  点击上传按钮   -->  
    <form id="upload-form" enctype="multipart/form-data">  
        <input type="file" id="upload" name="upload"/>  
    </form>  
</div>  
</body>  
</html>  
<style>  
    body{  
        width:100%;  
        height:auto;  
        margin:50px auto;  
        text-align:center;  
        background-color:#2EB3FF  
    }  
</style>
分享
微博
QQ
微信
回复
2024-10-24 15:10:46
相关问题
HarmonyOS web组件上传文件
250浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
232浏览 • 1回复 待解决
通过web上传图片并进行预览
973浏览 • 1回复 待解决
HarmonyOS 图片上传文件过大如何处理
35浏览 • 1回复 待解决
Web组件如何实现文件上传功能?
411浏览 • 1回复 待解决
HarmonyOS 图片上传
22浏览 • 1回复 待解决
HarmonyOS 上传图片问题
41浏览 • 1回复 待解决
HarmonyOS 如何上传文件
46浏览 • 1回复 待解决
HarmonyOS上传文件问题
558浏览 • 1回复 待解决
HarmonyOS使用Web组件预览PDF图片
753浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
482浏览 • 1回复 待解决
HarmonyOS 图片上传httpRequest格式问题
38浏览 • 1回复 待解决
HarmonyOS 上传文件相关Demo
495浏览 • 1回复 待解决
HarmonyOS文件上传怎么写
545浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
467浏览 • 1回复 待解决
HarmonyOS日志文件上传问题
576浏览 • 1回复 待解决
HarmonyOS 如何上传本地图片
111浏览 • 1回复 待解决
HarmonyOS 文件上传 & 资源读取问题
546浏览 • 1回复 待解决
怎么实现后台上传图片
2337浏览 • 1回复 待解决