国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

React Native Expo項(xiàng)目上傳文件

這篇具有很好參考價(jià)值的文章主要介紹了React Native Expo項(xiàng)目上傳文件。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

React Native Expo項(xiàng)目上傳文件,web前端,react native,react.js,javascript

React Native Expo項(xiàng)目上傳文件,web前端,react native,react.js,javascript?

?React Native Expo項(xiàng)目上傳文件,web前端,react native,react.js,javascript

?

Index.js:

import React from 'react'
import {
  ScrollView,
  View,
  TouchableWithoutFeedback,
  Text,
  Image,
} from 'react-native'
import { WebView } from 'react-native-webview'
import style from './style.js'
import { Icon } from '../../../component/light'
import { Divider, LinearProgress, Button, Input, Dialog } from '@rneui/themed'
import useList from './useList.js'

export default function Index(props) {
  const {
    username,
    emailCode,
    isSendEmail,
    count,
    avatar,
    nickname,
    password,
    code,
    visible,
    isLoading,
    captcha,
    visible1,
    usernameEl,
    setEmailCode,
    handleSendEmail,
    setNickname,
    setPassword,
    setCode,
    toggleDialog1,
    handleInput,
    handleLogin,
    handleNav,
    handleVisilbe,
    handleUploadAvatar,
    getCaptcha,
    pickFile,
  } = useList(props)

  return (
    <ScrollView style={style.mRegisterWrap}>
      <LinearProgress
        style={[style.mLoading, isLoading ? style.mLoadingActive : {}]}
        color="primary"
      />
      <View style={style.mRegisterInner}>
        <View style={style.mRegisterRow}>
          <Input
            label="郵箱"
            style={style.mRegisterInput}
            labelStyle={style.mRegisterLabelStyle}
            value={username}
            onChangeText={handleInput}
            placeholder="郵箱"
            // autoFocus
            ref={usernameEl}
            rightIcon={
              <Icon
                name={'help'}
                onPress={toggleDialog1}
                style={style.mInpntRightIcon}
              ></Icon>
            }
          ></Input>
        </View>
        <View style={style.mRegisterRow}>
          <Input
            label="郵箱驗(yàn)證碼"
            style={style.mRegisterInput}
            labelStyle={style.mRegisterLabelStyle}
            value={emailCode}
            onChangeText={setEmailCode}
            placeholder="郵箱驗(yàn)證碼"
            rightIcon={
              isSendEmail ? (
                <Button
                  title={`${count}秒后重新發(fā)送`}
                  disabled
                  containerStyle={style.mSendEmailBtn}
                />
              ) : (
                <Button
                  title="發(fā)送"
                  containerStyle={style.mSendEmailBtn}
                  onPress={handleSendEmail}
                />
              )
            }
          ></Input>
        </View>
        <View style={style.mRegisterRow}>
          <View style={style.mRegisterAvavtarTextWrap}>
            <Text style={style.mRegisterAvavtarText}>頭像</Text>
          </View>
          {avatar ? (
            <TouchableWithoutFeedback onPress={handleUploadAvatar}>
              <Image
                source={{ uri: avatar }}
                style={style.mRegisterAvatar}
              ></Image>
            </TouchableWithoutFeedback>
          ) : (
            <View style={style.mRegisterUploadIcoWrap}>
              <Icon
                name={'add'}
                onPress={handleUploadAvatar}
                style={style.mRegisterUploadIcon}
              ></Icon>
            </View>
          )}
        </View>
        <View style={style.mRegisterRow}>
          <Input
            label="昵稱"
            style={style.mRegisterInput}
            labelStyle={style.mRegisterLabelStyle}
            value={nickname}
            onChangeText={setNickname}
            placeholder="昵稱"
          ></Input>
        </View>
        <View style={style.mRegisterRow}>
          <Input
            label="密碼"
            style={style.mRegisterInput}
            labelStyle={style.mRegisterLabelStyle}
            value={password}
            onChangeText={setPassword}
            placeholder="密碼"
            secureTextEntry={!visible}
          ></Input>
          <Icon
            name={visible ? 'show' : 'close'}
            onPress={handleVisilbe}
            style={style.mRegisterPasswordIcon}
          ></Icon>
        </View>
        <View style={style.mRegisterRow}>
          <Input
            label="驗(yàn)證碼"
            style={style.mRegisterInput}
            labelStyle={style.mRegisterLabelStyle}
            value={code}
            onChangeText={setCode}
            placeholder="驗(yàn)證碼"
          ></Input>
        </View>
        <View style={style.mRegisterCodeWrap}>
          <TouchableWithoutFeedback onPress={getCaptcha}>
            <WebView
              originWhitelist={['*']}
              source={{ html: captcha }}
            ></WebView>
          </TouchableWithoutFeedback>
        </View>

        <View style={[style.mRegisterBtnWrap]}>
          <Button onPress={handleLogin} type="solid" title="注冊(cè)"></Button>
        </View>
        <View style={style.mRegisterMoreBtnWrap}>
          <Button
            containerStyle={style.mRegisterMoreBtn}
            onPress={() => handleNav('Login')}
            title="登錄"
            type="clear"
          ></Button>
          <Button
            containerStyle={style.mRegisterMoreBtn}
            onPress={() => handleNav('IndexForTab')}
            title="游客"
            type="clear"
          ></Button>
        </View>
        <Divider width={1}></Divider>
        <Button title="上傳文件" onPress={pickFile} />

        <Text style={style.mRegisterText}>注冊(cè)</Text>

      </View>
      <Dialog isVisible={visible1} onBackdropPress={toggleDialog1}>
        <Dialog.Title title="提示" />
        <Text>某些用戶的郵件會(huì)發(fā)送到垃圾箱</Text>
      </Dialog>
    </ScrollView>
  )
}

useList.js:

import { useState, useRef, useEffect } from 'react'
import Api from '../../../api'
import Constants from 'expo-constants'
import Toast from 'react-native-root-toast'
import { checkEmail } from '../../../utils/tools'
import AsyncStorage from '@react-native-async-storage/async-storage'
import * as ImagePicker from 'expo-image-picker'
import * as DocumentPicker from 'expo-document-picker'

let timer
export default function useList(props) {
  const [username, setUsername] = useState('')
  const [emailCode, setEmailCode] = useState('')
  const [isSendEmail, setIsSendEmail] = useState(false)
  const [count, setCount] = useState(0)
  const [emailId, setEmailId] = useState('')
  const [avatar, setAvatar] = useState(null)
  const [nickname, setNickname] = useState('')

  const [password, setPassword] = useState('')
  const [code, setCode] = useState('')
  const [visible, setVisible] = useState(false)
  // eslint-disable-next-line
  const [isLoading, setIsLoading] = useState(false)
  const [captchaId, setCaptchaId] = useState('')
  const [captcha, setCaptcha] = useState('')
  const [visible1, setVisible1] = useState(false)
  const usernameEl = useRef(null)

  const { navigation } = props

  const toggleDialog1 = () => {
    setVisible1(!visible1)
  }
  const handleInput = (e) => {
    setUsername(e)
  }

  const handleSendEmail = () => {
    const { isEmail, message } = checkEmail(username)
    if (username.trim() === '') {
      Toast.show('郵箱不能為空', {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    } else if (isEmail === false) {
      Toast.show(message, {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    }

    Api.h5.userSendEmailCode({ username }).then((res) => {
      if (res.code === 200) {
        setEmailId(res.data.emailId)
        Toast.show(res.message, {
          duration: 3000,
          position: Toast.positions.CENTER,
        })
        setCount(Constants.manifest.extra.REACT_APP_MODE === 'dev' ? 6 : 180)
        setIsSendEmail(true)
        console.log('send11', res.data.emailId)
      }
    })
  }

  const handleLogin = () => {
    const { isEmail, message } = checkEmail(username)

    if (username.trim() === '') {
      Toast.show('用戶名不能為空', {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    } else if (isEmail === false) {
      Toast.show(message, {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    } else if (password.trim() === '') {
      Toast.show('密碼不能為空', {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    } else if (code.trim() === '') {
      Toast.show('驗(yàn)證碼不能為空', {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    } else if (!emailId) {
      Toast.show('請(qǐng)獲取郵箱驗(yàn)證碼', {
        duration: 3000,
        position: Toast.positions.CENTER,
      })
      return
    }

    Api.h5
      .userAiAdd({
        username,
        emailCode,
        emailId,
        nickname,
        password,
        code,
        captchaId,
      })
      .then((res) => {
        if (res.code === 200) {
          navigation.navigate('Login')
        }
      })
  }

  const handleNav = (path) => {
    navigation.navigate(path)
  }

  const handleVisilbe = () => {
    setVisible(!visible)
  }

  const handleUploadAvatar = async () => {
    try {
      let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
        base64: true,
        //allowsEditing: true,
        //aspect: [4, 3],
        //quality: 1,
      })
      if (!result.canceled) {
        const formData = new FormData()
        let uri = result.assets[0].uri
        let uriArr = uri.split('/')
        let name = uriArr[uriArr.length - 1]
        console.log(uri)
        setAvatar(uri)
        formData.append('file', {
          uri,
          name,
          //type: result.assets[0].type,
          type: 'image/jpeg',
        })
        Api.h5
          .uploadFile(formData)
          .then((res) => {
            console.log(res)
            if (res.code === 200) {
              console.log('成功')
              Toast.show('上傳成功', {
                duration: 3000,
                position: Toast.positions.CENTER,
              })
            }
          })
          .catch((err) => {
            Toast.show('上傳失敗', {
              duration: 3000,
              position: Toast.positions.CENTER,
            })
          })
      } else {
        console.log('取消文件選擇')
      }
    } catch (error) {
      console.log('選擇文件時(shí)出錯(cuò)', error)
    }
  }

  const getCaptcha = () => {
    console.log('captcha')
    Api.h5.userCaptcha({}).then((res) => {
      if (res.code === 200) {
        const { captchaId, captcha } = res.data
        let svg = captcha
        let height = svg.indexOf('height')
        let width = svg.indexOf('width')
        let step1 = svg.slice(0, height + 8)
        let step2 = svg.slice(height + 8 + 2)
        svg = `${step1}150${step2}`
        let step3 = svg.slice(0, width + 5)
        let step4 = svg.slice(width + 8 + 3)
        svg = `${step3}450${step4}`
        let html = `<div style="text-align:center;width:100%;overflow:hidden;">${svg}</div>`
        setCaptcha(html)
        setCaptchaId(captchaId)
      }
    })
  }

  const pickFile = async () => {
    try {
      const result = await DocumentPicker.getDocumentAsync()

      if (result.type === 'success') {
        // 執(zhí)行文件上傳的邏輯
        console.log(result.uri) // 上傳文件的URI
        console.log(result.name) // 上傳文件的名稱
        console.log(result.size) // 上傳文件的大小
        console.log(result.mimeType) // 上傳文件的類型

        const formData = new FormData()
        let uri = result.uri
        let uriArr = uri.split('/')
        let name = uriArr[uriArr.length - 1]

        formData.append('file', {
          uri: result.uri,
          name: result.name,
          type: result.mimeType,
        })
        Api.h5
          .uploadFile(formData)
          .then((res) => {
            console.log(res)
            if (res.code === 200) {
              console.log('成功')
              Toast.show('上傳成功', {
                duration: 3000,
                position: Toast.positions.CENTER,
              })
            }
          })
          .catch((err) => {
            Toast.show('上傳失敗', {
              duration: 3000,
              position: Toast.positions.CENTER,
            })
          })
      } else {
        console.log('取消文件選擇')
      }
    } catch (error) {
      console.log('選擇文件時(shí)出錯(cuò)', error)
    }
  }

  useEffect(() => {
    ;(async () => {
      // 請(qǐng)求權(quán)限
      const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync()
      if (status !== 'granted') {
        console.log('拒絕訪問圖片庫權(quán)限!')
      } else {
        console.log('已經(jīng)獲得訪問圖片庫權(quán)限123!')
      }
    })()
  }, [])

  useEffect(() => {
    getCaptcha()
  }, [])

  useEffect(() => {
    clearTimeout(timer)
    timer = setTimeout(() => {
      if (count > 1) {
        setCount(count - 1)
      } else {
        setIsSendEmail(false)
      }
    }, 1000)
    // eslint-disable-next-line
  }, [count])

  useEffect(() => {
    //usernameEl.current.focus()
    //console.log(666, usernameEl.current.isFocused())
  }, [])

  useEffect(() => {
    Api.h5.uploadGetTokenForH5().then(async (res) => {
      if (res.code === 200) {
        await AsyncStorage.setItem('qiniuUploadTokenForH5', res.data.token)
        const qiniuUploadTokenForH5 = await AsyncStorage.getItem(
          'qiniuUploadTokenForH5'
        )
        console.log('qiniuUploadTokenForH5', qiniuUploadTokenForH5)
      }
    })
  }, [])

  return {
    username,
    emailCode,
    isSendEmail,
    count,
    avatar,
    nickname,
    password,
    code,
    visible,
    isLoading,
    captcha,
    visible1,
    usernameEl,
    setEmailCode,
    handleSendEmail,
    setNickname,
    setPassword,
    setCode,
    toggleDialog1,
    handleInput,
    handleLogin,
    handleNav,
    handleVisilbe,
    handleUploadAvatar,
    getCaptcha,
    pickFile,
  }
}

app.json:

{
  "expo": {
    "plugins": [
      [
        "expo-document-picker",
        {
          "iCloudContainerEnvironment": "Production"
        }
      ]
    ]
  }
}

React Native Expo項(xiàng)目上傳文件,web前端,react native,react.js,javascript?

?

參考鏈接:

https://chat.xutongbao.top/

https://docs.expo.dev/versions/latest/sdk/document-picker/

https://blog.csdn.net/xutongbao/article/details/131981469?spm=1001.2014.3001.5501?文章來源地址http://www.zghlxwxcb.cn/news/detail-616608.html

到了這里,關(guān)于React Native Expo項(xiàng)目上傳文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Expo項(xiàng)目 使用Native base UI庫

    Expo項(xiàng)目 使用Native base UI庫

    裝包: yarn add native-base expo install react-native-svg@12.1.1 ? Index.js: ? ? 參考鏈接: https://docs.nativebase.io/install-expo https://chat.xutongbao.top/

    2024年02月13日
    瀏覽(22)
  • React Native 集成到iOS原有的項(xiàng)目上

    React Native 集成到iOS原有的項(xiàng)目上

    集成到現(xiàn)有原生應(yīng)用 把 React Native 組件集成到 iOS 應(yīng)用中有如下幾個(gè)主要步驟: 配置好 React Native 依賴和項(xiàng)目結(jié)構(gòu)。 了解你要集成的 React Native 組件。 使用 CocoaPods,把這些組件以依賴的形式加入到項(xiàng)目中。 創(chuàng)建 js 文件,編寫 React Native 組件的 js 代碼。 在應(yīng)用中添加一個(gè)RC

    2024年02月12日
    瀏覽(20)
  • 探索React Native認(rèn)證實(shí)戰(zhàn)示例項(xiàng)目:打造安全的移動(dòng)應(yīng)用體驗(yàn)

    項(xiàng)目地址:https://gitcode.com/hezhii/react-native-auth-example 在移動(dòng)開發(fā)領(lǐng)域,React Native以其跨平臺(tái)和高效性能而備受青睞。如果你正在尋找一個(gè)直觀的、基于React Native的身份驗(yàn)證實(shí)現(xiàn)示例,那么這個(gè)項(xiàng)目—— react-native-auth-example ,將會(huì)是你的理想之選。 react-native-auth-example 是一個(gè)簡(jiǎn)單

    2024年04月27日
    瀏覽(29)
  • 重啟React Native老項(xiàng)目的奇幻之旅:填坑實(shí)錄與解決方案分享

    重啟React Native老項(xiàng)目的奇幻之旅:填坑實(shí)錄與解決方案分享

    這兩天為了重啟五年前基于 React Native(版本 0.59.9)開發(fā)的老項(xiàng)目,經(jīng)過各種填坑查詢等操作,最終把它成功地運(yùn)行起來了。 在這篇文章中,我將詳述那些遭遇的挑戰(zhàn)以及對(duì)應(yīng)的解決方案,以期為同樣面臨此類困境的開發(fā)者提供寶貴的經(jīng)驗(yàn)參考。 這個(gè)項(xiàng)目涉及到的環(huán)境基本版

    2024年04月16日
    瀏覽(20)
  • react native 0.73 配置 react-native-fs

    npm yarn android/settings.gradle android/app/build.gradle androidappsrcmainjavacomreactnative_demoMainApplication.kt 把原代碼 改為

    2024年04月08日
    瀏覽(75)
  • 探索React Native的世界:gorhom/react-native-animated-tabbar

    項(xiàng)目地址:https://gitcode.com/gorhom/react-native-animated-tabbar 在移動(dòng)應(yīng)用開發(fā)領(lǐng)域,React Native以其高效、跨平臺(tái)的能力受到了廣泛的歡迎。今天,我們要向您推薦一個(gè)極具創(chuàng)意且實(shí)用的React Native組件庫——gorhom/react-native-animated-tabbar。它是一個(gè)精美設(shè)計(jì)的動(dòng)畫TabBar,為您的應(yīng)用提供生

    2024年04月17日
    瀏覽(24)
  • React與React Native的異同

    開發(fā)React一段時(shí)間了,一直沒有搞清楚React和React Native的差異。今天特意去了解下,發(fā)現(xiàn)差異還真不?。?相同點(diǎn): 1.都是Facebook公司推出的框架。 2.都是基于JSX語言開發(fā)的。 差異點(diǎn): 1、作用的平臺(tái)不同. 2、工作原理不同. 3、渲染周期不同. 4、組件構(gòu)成形式不同. 5、宿主平臺(tái)的

    2024年02月06日
    瀏覽(24)
  • 維護(hù)積極的react native,為什么會(huì)有人造謠react native不維護(hù)了,停止維護(hù)。

    維護(hù)積極的react native,為什么會(huì)有人造謠react native不維護(hù)了,停止維護(hù)。

    ? ? ? ? 其實(shí)近幾年我一直關(guān)注react -native,他一直更新頻繁,0.60大重構(gòu),升級(jí)了js執(zhí)行引擎Hermes,當(dāng)前已經(jīng)0.70.4版本了。性能越來越提高,但是總感覺到有人在刷百度,只要輸入react-native后面就自動(dòng)提示熱搜“react-native 停止維護(hù)”,這誤導(dǎo)很多人以為真的不維

    2024年02月11日
    瀏覽(29)
  • 工欲善其事,必先利其器之—react-native-debugger調(diào)試react native應(yīng)用

    工欲善其事,必先利其器之—react-native-debugger調(diào)試react native應(yīng)用

    調(diào)試react應(yīng)用通常利用chrome的inspector的功能和兩個(gè)最常用的擴(kuò)展 1、React Developer Tools (主要用于debug組件結(jié)構(gòu)) 2、Redux DevTools (主要用于debug redux store的數(shù)據(jù)) 對(duì)于react native應(yīng)用,我們一般就使用react-native-debugger了,它是一個(gè)獨(dú)立的應(yīng)用,需要單獨(dú)安裝,在mac下可以用如下命令

    2024年02月16日
    瀏覽(48)
  • RN(React Native)

    RN(React Native)

    ?RN是React的原生組件庫 網(wǎng)址:React Native 中文網(wǎng) · 使用React來編寫原生應(yīng)用的框架 一、 ReactNative開發(fā)環(huán)境的搭建 ? 在本機(jī)搭建RN項(xiàng)目開發(fā)運(yùn)行環(huán)境 ?—— 3GB+ ?①安裝必需的編譯軟件:Node.js + JDK ?②安裝Android原生App開發(fā)軟件 —— Android?Studio ?③使用Android開發(fā)軟件下載編譯必

    2024年02月07日
    瀏覽(36)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包