2

I want to be able to post a multipart/form-data message back to the server. Now I know I can't send a file directly with an ajax call and I dont want to send any actual file. 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.

For example on the backend (php) I want to use echo $_FILES['uploadedfile']['name'] and see 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. Right now my ajax call looks like this:

        $.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);
            }
        });
Justin808
  • 20,859
  • 46
  • 160
  • 265
  • possible duplicate of [XMLHttpRequest POST multipart/form-data](http://stackoverflow.com/questions/155371/xmlhttprequest-post-multipart-form-data) ... though there afaik XMLHttpRequest2 offers ways to upload files. – Felix Kling Mar 15 '12 at 01:45
  • FelixKling - I don't want to use iframes, flash, or the ilk. I don't even want to upload a file. I need no access to any files. I want to fake the whole process in the jquery ajax call. – Justin808 Mar 15 '12 at 01:52

1 Answers1

0

you can use FormData to patch jquery, https://github.com/francois2metz/html5-formdata and see this question Sending multipart/formdata with jQuery.ajax

Community
  • 1
  • 1
viyancs
  • 2,289
  • 4
  • 39
  • 71