2013-10-25 72 views
0

发送PDF格式的服务器我想用我ajax.But无法找到jQuery的:通过AJAX

这problem.How可我得到的解决方案的任何实例或代码的PDF文件发送到服务器请帮忙我

+1

[何灿我上传 - 文件 - 异步与 - jQuery的(http://stackoverflow.com/questions/166221/how- can-i-upload-files-asynchronously-with-jquery) – mithunsatheesh

回答

0

有很好的教程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; 

} 
+0

教程链接将您从日本发送到不相关的网站。链接不好。 – OzzyTheGiant

0

您现在可以使用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); 
    } 
    }); 
}