4

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

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

asna
  • 242
  • 1
  • 3
  • 12
  • 1
    [how-can-i-upload-files-asynchronously-with-jquery](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) – Mithun Satheesh Oct 25 '13 at 04:14

2 Answers2

0

There is good tutorial 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;

}
Deepak Kumar
  • 413
  • 1
  • 4
  • 11
0

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

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

And the 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);
    }
  });
}
Charlie Martin
  • 8,208
  • 3
  • 35
  • 41