5

I'd want to send a FormData by using jQuery AJAX, like:

var uploadFormData = new FormData();
uploadFormData.append("name","value");

$.ajax({
    url : "(URL_target)",
    type : "POST",
    data : uploadFormData,
    cache : false,
    contentType : false,
    processData : false,
    success : function(r) {
        alert("Success!");
    }
});

But I also want to send a binary data by using jQuery AJAX, like:

var data = (...);

$.ajax({
    url: "(URL_target)",
    type: "POST",
    data : data,
    cache : false,
    contentType: "application/octet-stream",
    processData: false,
    success : function(r) {
        alert("Success!");
     }
});

How can I combine them into one data and send it out?

1 Answer 1

12

You can append binary data to FormData object as a Blob, File, ArrayBuffer object, or data URI

var uploadFormData = new FormData();
var data = (...);
uploadFormData.append("name","value");
uploadFormData.append("data", new Blob([data], {type:"application/octet-stream"}));

$.ajax({
  url : "(URL_target)",
  type : "POST",
  data : uploadFormData,
  cache : false,
  contentType : false,
  processData : false,
  success : function(r) {
    alert("Success!");
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.