Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending FormData with a binary data by using jQuery AJAX

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?

like image 900
Banana Code Avatar asked Aug 30 '16 04:08

Banana Code


1 Answers

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!");
  }
});
like image 91
guest271314 Avatar answered Oct 09 '22 15:10

guest271314