简体   繁体   English

是否可以通过jquery ajax调用伪造multipart / form-data帖子?

[英]Is it possible to fake a multipart/form-data post with a jquery ajax call?

I want to be able to post a multipart/form-data message back to the server. 我希望能够将multipart / form-data消息发布回服务器。 Now I know I can't send a file directly with an ajax call and I dont want to send any actual file. 现在,我知道我无法直接通过ajax调用发送文件,并且我也不想发送任何实际文件。 I want to format the post so that it simulates a file transfer with this is the file data string as if it were the file's contents and test.txt as if it were the file name. 我想格式化该帖子,以便它模拟文件传输, this is the file data字符串,就好像它是文件的内容,而test.txt就像它是文件名。

For example on the backend (php) I want to use echo $_FILES['uploadedfile']['name'] and see test.txt . 例如在后端(php)上,我想使用echo $_FILES['uploadedfile']['name']并查看test.txt

I assume I'll have to muck with the headers being sent but not sure exactly what I have to set. 我认为我必须处理发送的标头,但是不确定我必须设置什么。 I also assume I'll have to treat the fake file data differently than the rest of the data I'm sending over the ajax call. 我还假设我将不得不与处理通过ajax调用发送的其余数据不同地对待伪造的文件数据。 Right now my ajax call looks like this: 现在我的ajax调用看起来像这样:

        $.ajax({
            beforeSend: function(req) {
                req.setRequestHeader("Accept", '');
                req.setRequestHeader("Accept", $('#type').val());
            },
            'url': $('#url').val(),
            'type': $('#verb').val(),
            'data': data,
            'mimeType': 'multipart/form-data',
            'complete': function (jqXHR, textStatus) {
                var msg = "Data: " + dump(data);
                msg += "<br /><br />Status: " + jqXHR.status + " (" + jqXHR.statusText + " - " + textStatus + ")<br />";
                msg += jqXHR.getAllResponseHeaders().replace(/\n/g, "<br />");
                msg += "---<br />" + jqXHR.responseText;
                $('#results').html(msg);
            }
        });

您可以使用FormData修补jquery, https://github.com/francois2metz/html5-formdata并查看此问题, 使用jQuery.ajax发送multipart / formdata

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM