将文件名输入myXhr.upload

时间:2017-03-23 08:45:43

标签: javascript ajax file-upload

我正在上传像这样的照片:

$.ajax({
    // Your server script to process the upload
    url: 'upload.php',
    type: 'POST',

    // Form data
    data: new FormData($('form')[0]),

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    // Custom XMLHttpRequest
    xhr: function() {
        var myXhr = $.ajaxSettings.xhr();
        if (myXhr.upload) {
            // For handling the progress of the upload
            myXhr.upload.addEventListener('progress', function(e) {
                console.log(myXhr);
                if (e.lengthComputable) {
                    $('progress').attr({
                        value: e.loaded,
                        max: e.total
                    });
                }
            } , false);
        }
        return myXhr;
    }
});

我想知道当前上传文件的名称及其输入名称为“myXhr.upload.addEventListener('progress',function(e){”function。

这可以在这里知道吗?

1 个答案:

答案 0 :(得分:0)

您可以在progress事件中找到输入:

var inputFile = $('form').find("input[type=file]");
var fileName = inputFile[0].files[0].name;

https://jsfiddle.net/a264am6m/