第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在循環(huán)內(nèi)使用回調(diào)?

如何在循環(huán)內(nèi)使用回調(diào)?

慕妹3242003 2021-10-14 14:27:21
背景是我允許用戶將多個(gè)文件拖入 Dropzone。我需要檢查每種文件類型。如果其中一個(gè)不允許,設(shè)置消息并早點(diǎn)離開。請(qǐng)參閱下面的代碼 Starts at for (let i = 0; i < acceptedFiles.length; i++) { Inside this for loop, I call reader.onloadend,這是一個(gè)回調(diào)。如何在 for 循環(huán)中運(yùn)行回調(diào)?// Keep it internal  const getMimetype = signature => {    switch (signature) {      case '89504E47':        return 'image/png';      case '47494638':        return 'image/gif';      case '25504446':        return 'application/pdf';      case 'FFD8FFDB':      case 'FFD8FFE0':        return 'image/jpeg';      case '504B0304':        return 'application/zip';      default:        return 'Unknown filetype';    }  };  const onDropAccepted = useCallback(acceptedFiles => {    // reset to default state    resetToDefaultState();    //test    console.log('acceptedFiles', acceptedFiles);    // reader    const reader = new FileReader();    let file;    // Multi    if (config.isMultipleFiles === true) {      // Loop all files and check file types      for (let i = 0; i < acceptedFiles.length; i++) {        file = acceptedFiles[i];        // get 1st 4 byptes        const blob = file.slice(0, 4);        reader.readAsArrayBuffer(blob);        reader.onloadend = evt => {          if (evt.target.readyState === FileReader.DONE) {            const uint = new Uint8Array(evt.target.result);            let bytes = [];            uint.forEach(byte => {              bytes.push(byte.toString(16));            });            const hex = bytes.join('').toUpperCase();            const type = getMimetype(hex);            // type is allowed            if (config.fileTypes.includes(type)) {              setFiles([...files, ...acceptedFiles]);            } else {              // type no good              setIsInvaildFileType(true);            }          }        };      }    } 
查看完整描述

1 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

我還必須以react-dropzone類似的方式驗(yàn)證每個(gè)文件。

我的解決方法是承諾FileReader.


1?? This is the promisified version of "FileReader"

const isValidFile = file => {

  return new Promise((resolve, reject) => {

    const reader = new FileReader();

    reader.onloadend = evt => {

      // other logic removed for brevity...


      2?? Your custom logic dictates, if the file is valid or not

      if (config.fileTypes.includes(type)) {

        resolve(true);

      } else {

        resolve(false);

      }

    };


    3?? Should there was an error, this file is not good.

    reader.onerror = () => resolve(false)


    4?? Start the reading process.

    const blob = file.slice(0, 4);

    reader.readAsArrayBuffer(blob);

  });

};

現(xiàn)在您可以在for您提到的循環(huán)中使用它。


const onDropAccepted = useCallback(acceptedFiles => {

  // reset to default state

  resetToDefaultState();


  1?? As `useCallback` accepts a non-async method,

      Create a wrapped async method we can call here.

  const processFiles = async () => {

    if (config.isMultipleFiles === true) {

      for (let i = 0; i < acceptedFiles.length; i++) {

        const file = acceptedFiles[i];

        2?? Here is where we validate the file using the code above.

        const isValid = await isValidFile(file);

        if (!isValid) {

          setIsInvaildFileType(true);

          return;

        }

      }


      3?? At this point, all files are good.

      setFiles([...files, ...acceptedFiles]);

    } else {

      // removed for brevity...

    }

  };


  4?? Let's fire up the process

  processFiles();

});


查看完整回答
反對(duì) 回復(fù) 2021-10-14
  • 1 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)