jQuery:Sending pdf to server via ajax
This question already has an answer here:
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;
}
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);
}
});
}
链接地址: http://www.djcxy.com/p/19616.html