javascript jQuery:通过 ajax 将 pdf 发送到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19580959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:02:01  来源:igfitidea点击:

jQuery:Sending pdf to server via ajax

javascriptjqueryajaxpdf

提问by asna

I want to send a pdf file to server using ajax.But I couldn't find any examples or codes for

我想使用 ajax 将 pdf 文件发送到服务器。但我找不到任何示例或代码

this problem.How can i get the solution?Please help me

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

回答by Deepak Kumar

There is good tutorial http://www.phpletter.com/DOWNLOAD/

有很好的教程http://www.phpletter.com/DOWNLOAD/

read and understand it will help you.

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

Anyways not my code but seems good way.

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

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;

}

回答by Charlie Martin

You can use the javascript FormData() object to do this now. I believe it works in everything except IE9 and below.

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

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

And the javascript..

和 javascript..

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);
    }
  });
}