Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为目录文件添加 rootId,以表示哪些文件属于同一个文件夹 #558

Closed
ultravires opened this issue Oct 11, 2020 · 0 comments

Comments

@ultravires
Copy link

拖拽上传时发现,如果多次拖拽相同文件夹 新建文件夹/hello-world.txt 进行上传,便无法获取文件列表中的文件关系。

假如有一个业务需求是:文件夹必须作为一个整体上传(但是后端并没有实现多文件上传),目前的解决方案是:第一个文件上传时会携带一个 id,后端根据这个 id 生成唯一的目录,如果后续文件的 id 与此一致,则统一放入目录中。

于是我修改了如下代码:

// 添加 DataTransfer
      addDataTransfer: function addDataTransfer(dataTransfer) {
            // ...
            if (item) {
              item.rootId = _this.generateUUID();
              items.push(item);
            }
          }
          // ...
      },

      // 获得 entry
      getEntry: function getEntry(entry) {
        // ...
        return new Promise(function (resolve, reject) {
          if (entry.isFile) {
            entry.file(function (file) {
              resolve([{
                size: file.size,
                name: path + file.name,
                type: file.type,
                file: file,
                rootId: entry.rootId // ========= //  add relative: rootId
              }]);
            });
          }
          // ...
      },


      // 生成 UUID
      generateUUID() {
        let d = new Date().getTime();
        if (window.performance && typeof window.performance.now === "function") {
          d += performance.now(); //use high-precision timer if available
        }
        let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
          let r = (d + Math.random() * 16) % 16 | 0;
          d = Math.floor(d / 16);
          return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
      }

不知这么做是否合理,还请各位指教一下。谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant