在ajax调用上上传空白PDF

I am trying to upload PDF using ajax. Ajax call is sent to API (It's REST API which is written by client which I can't mention here.) which uploads file. Here is my code:

    var settings = {
        beforeSend: function(xhrObj){
            xhrObj.setRequestHeader('Access-Control-Allow-Headers', '*');
            xhrObj.setRequestHeader("Cache-Control", "no-cache");
            xhrObj.setRequestHeader("Content-Type","application/octet-stream; charset=UTF-8");
            xhrObj.setRequestHeader("Accept","application/json;odata=verbose");
            xhrObj.setRequestHeader("Content-Transfer-Encoding", "base64");
            xhrObj.setRequestHeader("Authorization", self.makeAuth());
            xhrObj.setRequestHeader('Access-Control-Allow-Origin', '*');
        },
        async: false,
        processData: false,
        cache: false,
        crossDomain: true,
        url: url,
        method: "POST",
        data: file
    };

    $.ajax(settings).done(function (response) {
        alert(response);
    });

Response I am getting is:

{"validRequest":true,"RequestMessage":"Success","EntityName":"Asset","EntityId":"004-e9cb6087-b365-4ab7-ba76-0ad65b4133e7","URL":"","APIVersion":"1.0.0"}

And after PDF upload call is transferred to get PDF link. Which is as follows:

     var settings = {
        beforeSend: function(xhrObj){
            xhrObj.setRequestHeader("Content-Type","application/json");
            xhrObj.setRequestHeader("Accept","application/json");
            xhrObj.setRequestHeader("Authorization", self.makeAuth());
        },
        "async": true,
        "crossDomain": true,
        "url": url,
        "method": "POST",
        data: JSON.stringify({
            "CampaignName":"",
            "AssetType":"PDF",
            "Location":"Existing",
            "AssetId":assetId,
            "WebLink":"",
            "PDFName": fileName,
            "EmailAddress":email,
            "Notifications":"",
            "AssetName":AssetName,
            "AllowDownload":download_pdf,
            "ExternalEmailId": external_email_id
        })
    };

    $.ajax(settings).done(function (response) {
        alert(response);
    });

This call giving me response as:

{"validRequest":true,"RequestMessage":"link_to_pdf","EntityName":"QuickCampaign","EntityId":"015-95f14ab4-f249-4bf5-87d4-d52ce5098c6a","URL":"","APIVersion":"1.0.0"}

I am not getting why it is uploading blank PDF. Please help

Per our discussion in chat, you are getting your file and converting it like this:

var file = $('.file')[0].files[0];
fileReader = new FileReader(),
fileReader.onloadend = readSuccess; 
function readSuccess(e) { 
  console.log(e); 
  file_res = e.target.result; 
} 
fileReader.readAsText(file);

Note from the docs for .readAsText()

The readAsText method is used to read the contents of the specified Blob or File. When the read operation is complete, the readyState is changed to DONE, the loadend is triggered, and the result attribute contains the contents of the file as a text string.

What you want is .readAsDataURL() which states:

The readAsDataURL method is used to read the contents of the specified Blob or File. When the read operation is finished, the readyState becomes DONE, and the loadend is triggered. At that time, the result attribute contains the data as a URL representing the file's data as a base64 encoded string.

You'll need to change your code to something more like:

var file = $('.file')[0];
var fileReader = new FileReader();

fileReader.onload = function () {
    var data = fileReader.result; // this one leaves the `data:application/pdf;base64,` at the beginning 
    $('#data').html(data);
    var base64 = data.replace(/^[^,]*,/, ''); // this one removes that front bit
    $('#base64').html(base64);        
};
fileReader.readAsDataURL(file.files[0]);

Note that you'll need to figure out which format your API requires, ie - with or without the data:application/pdf;base64, and use the value accordingly, see the comments in the code.

Here is a jsFiddle showing the difference in the results of each method