PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

Pause Stream untill ajax request complete

Open safoorsafdar opened this issue 10 years ago • 1 comments

In my recent project, I'm trying to pause stream using chunk till ajax receive the completed function call. here's my implementaion

/**
     *
     * ********************************/
    $('#import_potential').submit(function () {
        $(".import-btn").attr('disabled', 'disabled').html("Loading...");
        if ($('input[type=file]')[0].files.length) {
            $('input[type=file]').parse({
                config: {
                    chunkSize: insure_PotentialImport_js.getConfig('chunkSize'),
                    header: insure_PotentialImport_js.getConfig('header'),
                    skipEmptyLines: insure_PotentialImport_js.getConfig('skipEmptyLines'),
                    complete: insure_PotentialImport_js.getConfigComplete,
                    error: insure_PotentialImport_js.getConfigError,
                    chunk: insure_PotentialImport_js.getConfigChunk,
                    beforeFirstChunk: insure_PotentialImport_js.getBeforeFirstChunk
                },
                before: function (file, inputElem) {
                    console.log("Parsing file...", file);
                },
                error: function (err, file) {
                    console.log("ERROR:", err, file);
                },
                complete: function () {
                    console.log("Completed");
                }
            });
        }
        return false;
    });
    insure_PotentialImport_js = {
        _default: {
            delimiter: "",  // auto-detect
            newline: "",    // auto-detect
            header: true,
            dynamicTyping: false,
            preview: 0,
            encoding: "",
            worker: false,
            comments: false,
            step: undefined,
            download: false,
            skipEmptyLines: true,
            fastMode: undefined,
            beforeFirstChunk: undefined,
            withCredentials: undefined,
            chunkSize: 15360, //15kb
        },
        getConfig: function (what) {
            return this._default[what];
        },
        getConfigChunk: function (results, parser) {
            var thisInstance = insure_PotentialImport_js;
            console.log("Calling : getConfigChunk");
            thisInstance.postChunkDataImport(results.data,parser);

            //console.log(results);
            //var progress = row.meta.cursor;
            //var newPercent = Math.round(progress / size * 100);
            //if (newPercent === percent) return;
            //percent = newPercent;
            //console.log('percent = newpercent', percent);
            /*var thisInstance = insure_PotentialImport_js;
             console.log("Calling : getConfigChunk");
             postChunkDataImport(results.data);*/
        }, getConfigError: function (err, file) {
            console.log("Calling : getConfigError");
        }, getConfigComplete: function (results) {
            console.log("Calling : getConfigComplete");
        }, getBeforeFirstChunk: function (chunk) {
            //console.log("getBeforeFirstChunk:"+chunk);
            var index = chunk.match(/\r\n|\r|\n/).index;
            var headings = chunk.substr(0, index).split(',');
            //console.log(headings[1].replace("\"",""));
            //headings[1].replace(/(?:^|\.?)([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, "")
            //headings[1].replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
            //console.log(headings);
            return headings.join() + chunk.substr(index);
        }, postChunkDataImport: function (data,parser) {
            $.ajax({
                url: '/potential/ajax/import',
                type: 'post',
                data: {records: data},
                async: false,
                dataType: 'json',
                beforeSend:function(){
                    parser.pause();
                }
            }).complete(function (data) {
                parser.resume();
            });
        }
    }

But the problem is, my ajax request function postChunkDataImport goes to infinity call, look forward to hearing from anyone to fulfill such requirement or if there is any thing to explain it further. Thanks in advance.

safoorsafdar avatar Oct 16 '15 12:10 safoorsafdar

I have a similar question. I want to perform an Ajax request with the info from n rows. So wondering how to accumulate across n rows and dispatch the Ajax request when step only provides a single row.

morenoh149 avatar Jul 07 '20 02:07 morenoh149