blueimp jquery fileupload单个文件的进度

时间:2017-03-26 17:30:29

标签: jquery model-view-controller file-upload blueimp

在尝试在一个请求中发送选择的文件时,如何报告单个文件的上传进度,我遇到了问题。

进度事件数据似乎不包含任何指向正在上传的特定文件的指针。

这是我正在使用的代码:

public class Main {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        Path path = Paths.get("/home/user/workspace/JParser/myfolder/yourfolder/Hisfolder/a.txt");

        Instant start = Instant.now();
        Path resolution;
        try{
            resolution = caseCheck(path);
        }catch (Exception e) {
            throw new IllegalArgumentException("Couldnt access given path", e);
        }
        Instant end = Instant.now();

        Duration duration = Duration.between(start, end);

        System.out.println("Path is: " + resolution + " process took " + duration.toMillis() + "ms");

    }

    /**
     * @param path
     * @return
     * @throws IOException
     */
    private static Path caseCheck(Path path) throws IOException {

        Path entryPoint = path.isAbsolute() ? path.getRoot() : Paths.get(".");
        AtomicInteger counter = new AtomicInteger(0);
        while (counter.get() < path.getNameCount()) {
            entryPoint = Files
                    .walk(entryPoint, 1)
                    .filter(s -> checkPath(s, path, counter.get()))
                    .findFirst()
                    .orElseThrow(()->new IllegalArgumentException("No folder found"));

            counter.getAndIncrement();

        }

        return entryPoint;

    }

    /**
     * @param s
     * @param path
     * @param index
     * @return
     */
    private static final boolean checkPath(Path s, Path path, int index){
        if (s.getFileName() == null) {
            return false;
        }
        return s.getFileName().toString().equalsIgnoreCase(path.getName(index).toString());
    }
}

控制器操作返回如下

$('#fileupload')
.fileupload({
    dataType: "json",
    singleFileUploads: false, //to ensure that all files are in one request
    url: "/api/files/upload",
    progressInterval: 100,
    add: function(e, data) {
        $('#filelistholder').removeClass('hide');
        $(data.files)
            .each(function(index) {
                data.context = $('<div />').text(data.files[index].name).appendTo('#filelistholder');
                $('</div><div class="progress progress-striped active"><div class="progress-bar" role="progressbar"  style="width:0%"></div></div>').appendTo(data.context);
            });
        $('#btnUploadAll').click(function() {
                data.submit();
            });
    },
    progress: function(e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        //how to get progress for a current file?
    },
    processalways: { //process does not get called at all...
        function(e, data) {
            console.log('Process ' + data.files[data.index].name + ' ended.');
        }
    }
});

我也在这里看了一些关于SO的相关问题,比如 Blueimp file upload, how do you know which file the progress callback is for? 但我没有找到任何答案

提前致谢

0 个答案:

没有答案