【问题标题】:jQuery:Sending pdf to server via ajax [duplicate]jQuery:通过ajax将pdf发送到服务器[重复]
【发布时间】:2013-11-04 01:08:19
【问题描述】:

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

这个问题。我该如何解决?请帮助我

【问题讨论】:

标签: javascript jquery ajax pdf


【解决方案1】:

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

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

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

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;

}

【讨论】:

  • 教程链接会将您带到一个来自日本的不相关网站。链接不好。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 2014-09-09
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2018-02-27
    • 2021-10-13
    相关资源
    最近更新 更多