Javascript jQuery:通过ajax向服务器发送pdf

Javascript jQuery:通过ajax向服务器发送pdf,javascript,jquery,ajax,pdf,Javascript,Jquery,Ajax,Pdf,我想用ajax向服务器发送一个pdf文件,但找不到任何示例或代码 这个问题。我怎样才能得到解决方案?请帮助我。有很好的教程 阅读并理解它将对你有所帮助 无论如何,不是我的代码,但似乎是好办法 function ajaxFileUpload(){ //starting setting some animation when the ajax starts and completes $("#loading") .ajaxStart(function(){ $

我想用ajax向服务器发送一个pdf文件,但找不到任何示例或代码

这个问题。我怎样才能得到解决方案?请帮助我。

有很好的教程

阅读并理解它将对你有所帮助

无论如何,不是我的代码,但似乎是好办法

function ajaxFileUpload(){
    //starting setting some animation when the ajax starts and completes
    $("#loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });

    /*
        prepareing ajax file upload
        url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
        dataType: it support json, xml
        secureuri:use secure protocol
        success: call back function when the ajax complete
        error: callback function when the ajax failed

            */
    $.ajaxFileUpload
    (
        {
            url:'doajaxfileupload.php', 
            secureuri:false,
            fileElementId:'fileToUpload',
            dataType: 'json',
            success: function (data, status)
            {
                if(typeof(data.error) != 'undefined')
                {
                    if(data.error != '')
                    {
                        alert(data.error);
                    }else
                    {
                        alert(data.msg);
                    }
                }
            },
            error: function (data, status, e)
            {
                alert(e);
            }
        }
    )

    return false;

}

现在可以使用javascript FormData()对象来执行此操作。我相信它适用于除IE9及以下版本之外的所有版本

<form>
  <input type="file" id="file" name="file">
  <button onclick="upload()">Upload</button>
</form>

教程链接将您发送到一个来自日本的无关网站。链接是不好的。
function upload() {
  var fd = new FormData(),
      myFile = document.getElementById("file").files[0];

  fd.append( 'file',  myFile);

  $.ajax({
    url: 'http://example.com/script.php',
    data: fd,
    processData: false,
    contentType: false,
    type: 'POST',
    success: function(data){
      console.log(data);
    }
  });
}