ajax flash upload,ajax php file upload without iFrame or flash engine

I've simplified my code for uploading a file without iFrame or flash engine, and i came up to this ajax function:

function uploadFile(files) {

var xmlhttp;

if(window.XMLHttpRequest)

xmlhttp = new XMLHttpRequest();

else

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.upload.onprogress = function(e) {

$("#progress").empty().append(e.loaded + " - " + e.total);

}

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

alert(xmlhttp.responseText + "DONE!");

}

}

xmlhttp.open("post", "post.php", true);

xmlhttp.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");

xmlhttp.setRequestHeader("Cache-Control", "no-cache");

xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");

xmlhttp.setRequestHeader("X-File-Name", files[0].fileName);

xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");

xmlhttp.send(files[0]);

}

$(document).ready(function() {

$("#myfile").change(function() {

uploadFile(this.files);

});

});

This is the php code which reply to the ajax function:

if(isset(

$_SERVER['CONTENT_TYPE'],

$_SERVER['CONTENT_LENGTH'],

$_SERVER['HTTP_X_FILE_NAME']

) &&

$_SERVER['CONTENT_TYPE'] == 'multipart/form-data'){

$file->name = basename($_SERVER['HTTP_X_FILE_NAME']);

$input = fopen('php://input', 'rb');

$file = fopen('files/'.$file->name, 'wb');

stream_copy_to_stream($input, $file);

fclose($input);

fclose($file);

} else {

echo "Error";

}

?>

The problem is, sometimes it works sometimes it bugs up while trying to upload the same file. I hope there is a solution to fix this issue. The code is simple, when i choose a file with input file type, the uploadFile function executes.

When it bugs out, i can see the file starting to be uploaded but it doesnt have the original size, so somewhere it could bug and stop uploading.

Thank you in advance, Daniel!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值