5

我需要从 FormData 中读取数据吗?我尝试阅读类似someFormatData["valueName"]但它不起作用的东西。 options["fileId"]options["file"]不起作用。我也尝试options.fileId相同的结果:

function upload(file, fileId, callback) {
    var formData = new FormData();
    formData.append("file", file);
    formData.append("fileID", fileId);

    $.ajax({
        url: '/url',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(response) {
            callback(response);
        }
    });
}


asyncTest("test upload chunk", function() {
    var blob = new Blob(["Hello world!"], { type: "text/plain" }),        
        options = null,
        fileID ="someFileID",
        response;

    jQuery.ajax = function(param) {
        options = param;   // THIS is FormData object 
        // how to read fileId and file from here
    };

    upload(blob, fileID, function (data) {
        response = data;  
    });

    options.success({
        someProp: 'responseFromServer'
    });

    setTimeout(function() {
        QUnit.equal(options, "dataTosend", "parameters is OK");
        QUnit.equal(response["someProp"], "responseFromServer", "Response ok");
        start();
    },1000);
});
4

2 回答 2

5

如果你拿你的FormData对象,你可以在它上面使用几种不同的方法……你要找的是

formData.get()

或者

formData.getAll()

https://developer.mozilla.org/en-US/docs/Web/API/FormData

请注意,get()并非所有浏览器都完全支持该方法。

于 2015-06-01T04:34:32.167 回答
0

您可以使用此阅读

formData.get('fileId') // to read Id
formData.get('file') // to read the file 
于 2021-10-16T07:05:18.173 回答