【问题标题】:Axios post Large FileAxios 发布大文件
【发布时间】:2020-11-28 14:36:27
【问题描述】:

我正在尝试将一个大型 JSON 发布到我的数据库中,该 JSON 至少有 400.000 个对象。

如果我剪切文件并尝试发布 20.000 个对象,一切正常,所以问题应该是 JSON 的大小。

我已将 JSON 分成 20 个块,我的想法是一次上传一个,但我正在努力让它工作。

这是我正在使用的:

var rows = {};
Papa.parse(content, {
    header: false,
    delimiter: '|',
    worker: true,
    encoding: "utf16le",
    dynamicTyping: true,
    skipEmptyLines: true,
    complete: function(results) {
        rows = results.data;
        let obj = []
        for(var i=0; i < rows.length; i++){
            obj.push(rows[i])
        }

        let result = []

        for(let i in obj) {
            let temp = {}
            if(i > 0) {
                temp["id"] = obj[i][0]
                temp["name"] = obj[i][1]
                temp["tel"] = obj[i][2]
                temp["email"] = obj[i][3]
                temp["status"] = obj[i][5]
                
                result.push(temp)
            }
        }
        
        var array1 = result.map((e) => {
            return {
                id: e.id,
                name: e.name,
                email: e.email
            }
        })

        let chunked = []
        let size = 20000;

        Array.from({length: Math.ceil(array1.length / size)}, (val, i) => {
        chunked.push(array1.slice(i * size, i * size + size))
        })

        console.log(chunked); // at this point I have my array divided into chunks of 20000

        axios({
            url: 'url',
            method: 'post',
            data: chunked
          })
          .then(function (response) {
              // your action after success
              console.log(response);
          })
          .catch(function (error) {
             // your action on error successif (error.response) {
            console.log(error);
        
          });

【问题讨论】:

  • 您已经分成了 20 个块,即“分块”,但仍然发送整个数组,这与发送相同数量的数据相同,因为没有分割它。更好地循环遍历您的“分块”发送。
  • 你好,谢谢你的回答,我试过循环,我只是不知道如何一个一个发送,不管我做什么我想我总是一次发送整个数组

标签: javascript arrays axios chunks papaparse


【解决方案1】:

您可以按以下方式一一发送。假设您的后端能够接受数据格式。

    for(let i=0;i<chunked.length;i++){
          axios({
            url: 'url',
            method: 'post',
            data: chunked[i]
          })
          .then(function (response) {
              // your action after success
              console.log(response);
          })
          .catch(function (error) {
             // your action on error successif (error.response) {
            console.log(error);
        
          });
     }

或者一个现代的解决方案可以是使用Promise.all([])

let promiseArray = [];
for(let i=0;i<chunked.length;i++){
    promiseArray.push(axios({
       url: 'url',
       method: 'post',
       data: chunked[i]
    }))
}

Promise.all(promiseArray)
.then((responses) => {
  console.log(responses);   //Will return resolved/rejected promises in an array
})
.catch(error => { 
  console.error(error.response)
});;

你可以阅读更多关于Promise.all([])https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all的信息

【讨论】:

  • 我都试过了,它设法上传〜16.000然后死了:/
  • 您遇到什么错误?另外,检查服务器日志,也许后端可能会失败。
  • 我看不到错误,因为在终端中它向我显示了整个 JSON,因此我认为 api 可能有问题,我将代码发送给您,以便您查看是否有问题错了。
  • 我在 phpmyadmin 中也遇到了这个错误 - mysqli_real_connect(): (HY000/2002)
  • 对于 MySQL 错误,StackOverflow 上有类似的问题可能会有所帮助。 stackoverflow.com/questions/41881123/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 2018-10-13
  • 1970-01-01
  • 2021-12-15
  • 2021-01-05
  • 2021-09-18
相关资源
最近更新 更多