久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Javascript DataTransfer 項(xiàng)目不會(huì)通過異步調(diào)用持久化

Javascript DataTransfer items not persisting through async calls(Javascript DataTransfer 項(xiàng)目不會(huì)通過異步調(diào)用持久化)
本文介紹了Javascript DataTransfer 項(xiàng)目不會(huì)通過異步調(diào)用持久化的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我正在使用 Vuejs 和 DataTransfer 異步上傳文件,我希望允許一次拖放多個(gè)文件進(jìn)行上傳.

I am using Vuejs along with DataTransfer to upload files asynchronously, and I want to allow multiple files to be dragged and dropped for upload at once.

我可以進(jìn)行第一次上傳,但在上傳完成時(shí),Javascript 已經(jīng)收集垃圾或更改了 DataTransfer 項(xiàng)目對(duì)象.

I can get the first upload to happen, but by the time that upload is done, Javascript has either garbage collected or changed the DataTransfer items object.

我怎樣才能重做這個(gè)(或克隆事件/DataTransfer 對(duì)象),以便在整個(gè) ajax 調(diào)用過程中我仍然可以使用數(shù)據(jù)?

How can I rework this (or clone the event/DataTransfer object) so that the data is still available to me throughout the ajax calls?

我已按照 MDN 文檔了解如何使用 DataTransfer,但我很難將它應(yīng)用到我的具體案例中.我也嘗試過復(fù)制事件對(duì)象,正如您在我的代碼中看到的那樣,但它顯然不會(huì)進(jìn)行深度復(fù)制,只是傳遞引用,這沒有幫助.

I've followed the MDN docs on how to use DataTransfer but I'm having a hard time applying it to my specific case. I also have tried copying the event objects, as you can see in my code, but it obviously does not do a deep copy, just passes the reference, which doesn't help.

    methods: {
        dropHandler: function (event) {
            if (event.dataTransfer.items) {
                let i = 0;
                let self = this;
                let ev = event;

                function uploadHandler() {
                    let items = ev.dataTransfer.items;
                    let len = items.length;

                    // len NOW EQUALS 4

                    console.log("LEN: ", len);
                    if (items[i].kind === 'file') {
                        var file = items[i].getAsFile();
                        $('#id_file_name').val(file.name);
                        var file_form = $('#fileform2').get(0);
                        var form_data = new FormData(file_form); 

                        if (form_data) {
                            form_data.append('file', file);
                            form_data.append('type', self.type);
                        }

                        $('#file_progress_' + self.type).show();
                        var post_url = '/blah/blah/add/' + self.object_id + '/'; 
                        $.ajax({
                            url: post_url,
                            type: 'POST',
                            data: form_data,
                            contentType: false,
                            processData: false,
                            xhr: function () {
                                var xhr = $.ajaxSettings.xhr();
                                if (xhr.upload) {
                                    xhr.upload.addEventListener('progress', function (event) {
                                        var percent = 0;
                                        var position = event.loaded || event.position;
                                        var total = event.total;
                                        if (event.lengthComputable) {
                                            percent = Math.ceil(position / total * 100);
                                            $('#file_progress_' + self.type).val(percent);
                                        }
                                    }, true);
                                }
                                return xhr;
                            }
                        }).done((response) => {
                                i++;
                                if (i < len) {

                                    // BY NOW, LEN = 0.  ????

                                    uploadHandler();
                                } else {
                                    self.populate_file_lists();
                                }
                            }
                        );
                    }
                }

                uploadHandler();
            }
        },

推薦答案

一旦你調(diào)用了await,你就不再在函數(shù)的原始調(diào)用堆棧中了.這在事件偵聽器中尤其重要.

Once you call await you're no longer in the original call stack of the function. This is something that would matter particularly in the event listener.

我們可以用setTimeout重現(xiàn)同樣的效果:

We can reproduce the same effect with setTimeout:

dropZone.addEventListener('drop', async (e) => {
  e.preventDefault();
  console.log(e.dataTransfer.items);
  setTimeout(()=> {
    console.log(e.dataTransfer.items);
  })
});

例如拖動(dòng)四個(gè)文件會(huì)輸出:

For example, dragging four files will output:

DataTransferItemList?{0: DataTransferItem, 1: DataTransferItem, 2: DataTransferItem, 3: DataTransferItem, length: 4}  
DataTransferItemList?{length: 0}

事件發(fā)生后,狀態(tài)發(fā)生了變化,物品丟失了.

After the event had happened the state has changed and items have been lost.

有兩種方法可以處理這個(gè)問題:

There are two ways to handle this issue:

  • 復(fù)制項(xiàng)目并對(duì)其進(jìn)行迭代
  • 將異步作業(yè)(Promises)推送到數(shù)組中,稍后使用 Promise.all
  • 處理它們

第二種解決方案比在循環(huán)中使用 await 更直觀.另外,請(qǐng)考慮并行連接受到限制.使用數(shù)組,您可以創(chuàng)建塊來(lái)限制同時(shí)上傳.

The second solution is more intuitive than using await in the loop. Also, consider parallel connections are limited. With an array you can create chunks to limit simultaneous uploads.

function pointlessDelay() {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 1000);
  });
}

const dropZone = document.querySelector('.dropZone');

dropZone.addEventListener('dragover', (e) => {
  e.preventDefault();
});

dropZone.addEventListener('drop', async (e) => {
  e.preventDefault();
  console.log(e.dataTransfer.items);
  const queue = [];
  
  for (const item of e.dataTransfer.items) {
    console.log('next loop');
    const entry = item.webkitGetAsEntry();
    console.log({item, entry});
    queue.push(pointlessDelay().then(x=> console.log(`${entry.name} uploaded`)));
  }
  
  await Promise.all(queue);
});

body {
  font-family: sans-serif;
}

.dropZone {
  display: inline-flex;
  background: #3498db;
  color: #ecf0f1;
  border: 0.3em dashed #ecf0f1;
  border-radius: 0.3em;
  padding: 5em;
  font-size: 1.2em;
}

<div class="dropZone">
  Drop Zone
</div>

這篇關(guān)于Javascript DataTransfer 項(xiàng)目不會(huì)通過異步調(diào)用持久化的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測(cè)試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來(lái)更改 lt;input type=filegt; 的事件用于單元測(cè)試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測(cè)試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測(cè)試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 草樱av| 成人伊人 | 伊人中文字幕 | 天天射美女 | 免费一看一级毛片 | 国产精品久久久久久久久免费桃花 | 一级黄色网页 | 日韩欧美在线视频 | 本地毛片| www久久久 | 亚洲一区视频 | 91精品国产日韩91久久久久久 | 国产色婷婷| 看片网站在线 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 日韩高清一区 | 91社区在线观看高清 | 婷婷综合色 | 好姑娘高清在线观看电影 | 男女污污网站 | h视频免费观看 | 精精国产xxxx视频在线播放7 | 国产精品欧美一区二区三区 | 亚洲免费精品 | 日本精品一区 | xxx国产精品视频 | 国产一区在线免费 | 中文字幕一区二区三区在线观看 | 少妇一级淫片免费播放 | 日韩福利一区 | 91精品久久久久久久久99蜜臂 | av片免费观看| 电影午夜精品一区二区三区 | 一区二区不卡视频 | 国产视频久久 | 狠狠色综合久久婷婷 | 成人妇女免费播放久久久 | 中文字幕亚洲精品在线观看 | 在线成人av | 亚洲一区二区三区免费在线观看 | 欧美成人a|