React上传+ 5000文件 - 无法加载资源:NET :: ERR_INSUFFIFIED_RESOURCES

发布于 2025-02-12 22:51:13 字数 3450 浏览 0 评论 0原文

我正在使用Dropzone和React构建上传文件功能,我的问题是该应用程序应该支持数千张图像,一旦我得到大约1500张图像,它就会崩溃并停止发送请求,然后在浏览器中,我遇到了这个问题:

Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES

我看到了一些解决方案以创建批处理,但老实说不知道如何创建它,因为我使用async函数一个一个一个一个一个一个一个,这是我的代码:

const Dropzone =  ({stateChanger, folder, props, ...rest}) => {
  let container;
  async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
      await callback(array[index], index, array);
    }
  }

  let { id } = useParams();
  const [files, setFiles] = useState([]);
  const {getRootProps, getInputProps} = useDropzone({    
    onDrop: async acceptedFiles => {
      
      stateChanger(true)

      setFiles(acceptedFiles.map(file => Object.assign(file, {
        preview: URL.createObjectURL(file),
        uid: uuidv4()
      })));

      
      const url = `${API_URL}/projects/upload`;

      let requestArr = [];

      await asyncForEach(acceptedFiles, async (file) => {
        console.log('file',file)
        var formData = new FormData();
            formData.append('file', file);
            formData.append('projectId', id);
            formData.append('rootfolder', folder);
            console.log('formData',formData)
          requestArr.push(
              axios
                  .post(url,formData)
                  .then((rst) => {
                    console.log('rst',rst) 
                    var elem = document.getElementById(file.uid);
                    var html = elem.innerHTML;
                    elem.innerHTML = `<span class="upload-success" >Uploaded</span>`;
                  })
                  .catch((error) => { 
                    console.log('error', error);
                    var elem = document.getElementById(file.uid);
                    var html = elem.innerHTML;
                    elem.innerHTML = `<span class="upload-error" >Error uploading</span>`;
                  })
          );
      });

      Promise.all(requestArr).then(() => {
        console.log('resolved promise.all')
        stateChanger(false)
      });      

    }
  });
  
  const thumbs = files.map(file => (
    <div className="thumb" key={file.name}>
      <div className="thumbList">
      
        {file.path} - {file.size} bytes - <span id={file.uid}><span className="upload-pending" >Uploading</span></span>
      
      </div>
    </div>
  ));

  useEffect(() => {
    // Make sure to revoke the data uris to avoid memory leaks
    files.forEach(file => URL.revokeObjectURL(file.preview));
  }, [files]);

  return (
    <section className="container">
      <ToastContainer
        ref={ref => container = ref}
        className="toast-top-right"
      />
      <div {...getRootProps({className: 'dropzone'})}>
        <input {...getInputProps()} />
        <img src={uploadImage} />
        <br />
        <p>Drag and drop your files, or click the button to upload</p>
        <button className="btn primary-btn-active width50">Upload from computer</button>
      </div>
      <aside >
        {thumbs}
      </aside>
    </section>
  );
}

export default Dropzone

并且实现非常标准:

<Dropzone stateChanger={setNextDisable} folder={folder} />

有什么建议吗?

I'm building an upload files functionality using dropzone and react, my issue is that this application should support thousands of images and once I got about 1500 images it collapse and stop sending requests, then in the browser I got this issue:

Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES

I saw some workarounds to create batches, but honestly don't know how to create it, since I'm processing the uploads one by one using async functions, this is my code:

const Dropzone =  ({stateChanger, folder, props, ...rest}) => {
  let container;
  async function asyncForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
      await callback(array[index], index, array);
    }
  }

  let { id } = useParams();
  const [files, setFiles] = useState([]);
  const {getRootProps, getInputProps} = useDropzone({    
    onDrop: async acceptedFiles => {
      
      stateChanger(true)

      setFiles(acceptedFiles.map(file => Object.assign(file, {
        preview: URL.createObjectURL(file),
        uid: uuidv4()
      })));

      
      const url = `${API_URL}/projects/upload`;

      let requestArr = [];

      await asyncForEach(acceptedFiles, async (file) => {
        console.log('file',file)
        var formData = new FormData();
            formData.append('file', file);
            formData.append('projectId', id);
            formData.append('rootfolder', folder);
            console.log('formData',formData)
          requestArr.push(
              axios
                  .post(url,formData)
                  .then((rst) => {
                    console.log('rst',rst) 
                    var elem = document.getElementById(file.uid);
                    var html = elem.innerHTML;
                    elem.innerHTML = `<span class="upload-success" >Uploaded</span>`;
                  })
                  .catch((error) => { 
                    console.log('error', error);
                    var elem = document.getElementById(file.uid);
                    var html = elem.innerHTML;
                    elem.innerHTML = `<span class="upload-error" >Error uploading</span>`;
                  })
          );
      });

      Promise.all(requestArr).then(() => {
        console.log('resolved promise.all')
        stateChanger(false)
      });      

    }
  });
  
  const thumbs = files.map(file => (
    <div className="thumb" key={file.name}>
      <div className="thumbList">
      
        {file.path} - {file.size} bytes - <span id={file.uid}><span className="upload-pending" >Uploading</span></span>
      
      </div>
    </div>
  ));

  useEffect(() => {
    // Make sure to revoke the data uris to avoid memory leaks
    files.forEach(file => URL.revokeObjectURL(file.preview));
  }, [files]);

  return (
    <section className="container">
      <ToastContainer
        ref={ref => container = ref}
        className="toast-top-right"
      />
      <div {...getRootProps({className: 'dropzone'})}>
        <input {...getInputProps()} />
        <img src={uploadImage} />
        <br />
        <p>Drag and drop your files, or click the button to upload</p>
        <button className="btn primary-btn-active width50">Upload from computer</button>
      </div>
      <aside >
        {thumbs}
      </aside>
    </section>
  );
}

export default Dropzone

And the implementation pretty standard:

<Dropzone stateChanger={setNextDisable} folder={folder} />

Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2025-02-19 22:51:14

您为什么一次需要1500个图像?
用户可以一次只能在屏幕上专注于这么多 - 然后您的滚动条有效但不理想。
在许多地方的解决方案是一次将其分页(10、20、50)在屏幕上。
不仅仅是要麻烦 - 作为开发人员,您可能拥有比普通用户更强大的PC-因此,您的大多数用户很可能会遇到更多的瓶颈问题,性能问题。

我认为的解决方案是减少屏幕上的项目数量,因为拥有大量资产和20个项目和用户不能集中精力或不愿意去看或尝试尝试是没有意义的做事。

Why would you need 1500, 5000 images at once?
The user can focus on only so much on the screen at once - and then you have scroll bars which work but is not ideal.
The solution in many places is to have it paginated (10, 20, 50) items on screen at once.
More than that is asking for trouble - as a developer you might have a more powerful PC than an average user - so likely most of your users will have more bottleneck problems, performance issues.

The solution in my opinion would be to reduce the number of items on screen because it doesn't make sense to have that massive amount of assets, and more than a 20 items and user can't focus or bother to have a look or try to do stuff.

木緿 2025-02-19 22:51:14

资源消耗一直是需要研究的问题。大量数据的处理也是一个类似的问题。如果仅上传。包装并在上传前压缩。

Resource consumption has always been a problem that needs to be researched. The processing of large amounts of data is also a similar problem. if only upload. package and compress before uploading.

满身野味 2025-02-19 22:51:14

这个问题与Dropzone库无关。它只是意味着您的服务器,无论您使用什么,都无法处理此类流量/连接。如果您真的需要,则可能应在错误消息指出的情况下专用更多资源。

The problem is not related to Dropzone library. It just means your server, whatever you use, cannot handle such amount of traffic/connections. If you really need you should probably dedicate more resources as the error message states.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文