How to upload string as file with jQuery or other js framework

Using javascript, I have a file in string (got with ajax request).

How to upload it as file to server by another ajax request ?


You need to set the Content-type request header to multipart/form-data and play around with the format a little, I wrote this in Plain Ol’ JavaScript ™ but you could easily rework it for a library:

EDIT: had my coffee now, so modified it for jQuery (no-library version here):

// Define a boundary, I stole this from IE but you can use any string AFAIK
var boundary = "---------------------------7da24f2e50046";
var body = '--' + boundary + 'rn'
         // Parameter name is "file" and local filename is "temp.txt"
         + 'Content-Disposition: form-data; name="file";'
         + 'filename="temp.txt"rn'
         // Add the file's mime-type
         + 'Content-type: plain/textrnrn'
         // Add your data:
         + data + 'rn'
         + '--'+ boundary + '--';

    contentType: "multipart/form-data; boundary="+boundary,
    data: body,
    type: "POST",
    url: "",
    success: function (data, status) {

Source: stackoverflow