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组件上传文件
229浏览 • 1回复 待解决
HarmonyOS Web 图片上传功能失效
199浏览 • 1回复 待解决
通过web上传图片并进行预览
935浏览 • 1回复 待解决
Web组件如何实现文件上传功能?
384浏览 • 1回复 待解决
HarmonyOS上传文件问题
513浏览 • 1回复 待解决
HarmonyOS使用Web组件预览PDF图片
690浏览 • 1回复 待解决
HarmonyOS图片上传相关问题
445浏览 • 1回复 待解决
HarmonyOS 上传文件相关Demo
453浏览 • 1回复 待解决
HarmonyOS文件上传怎么写
509浏览 • 1回复 待解决
HarmonyOS如何实现图片上传
434浏览 • 1回复 待解决
HarmonyOS日志文件上传问题
529浏览 • 1回复 待解决
HarmonyOS 文件上传 & 资源读取问题
503浏览 • 1回复 待解决
HarmonyOS 文件上传服务器问题
323浏览 • 1回复 待解决
怎么实现后台上传图片
2320浏览 • 1回复 待解决
HarmonyOS如何上传图片到模拟器相册
867浏览 • 1回复 待解决
如何在HarmonyOS中实现图片上传功能?
186浏览 • 0回复 待解决