ajax提交图片太大,Ajax上传图片

小智..

17

HTML代码

Upload Photo :

这是Ajax代码:

function submitForm() {

var fcnt = $('#filecount').val();

var fname = $('#filename').val();

var imgclean = $('#file');

if(fcnt<=5)

{

data = new FormData();

data.append('file', $('#file')[0].files[0]);

var imgname = $('input[type=file]').val();

var size = $('#file')[0].files[0].size;

var ext = imgname.substr( (imgname.lastIndexOf('.') +1) );

if(ext=='jpg' || ext=='jpeg' || ext=='png' || ext=='gif' || ext=='PNG' || ext=='JPG' || ext=='JPEG')

{

if(size<=1000000)

{

$.ajax({

url: "<?php echo base_url() ?>/upload.php",

type: "POST",

data: data,

enctype: 'multipart/form-data',

processData: false, // tell jQuery not to process the data

contentType: false // tell jQuery not to set contentType

}).done(function(data) {

if(data!='FILE_SIZE_ERROR' || data!='FILE_TYPE_ERROR' )

{

fcnt = parseInt(fcnt)+1;

$('#filecount').val(fcnt);

var img = '

'+data+'
';

$('#prv').append(img);

if(fname!=='')

{

fname = fname+','+data;

}else

{

fname = data;

}

$('#filename').val(fname);

imgclean.replaceWith( imgclean = imgclean.clone( true ) );

}

else

{

imgclean.replaceWith( imgclean = imgclean.clone( true ) );

alert('SORRY SIZE AND TYPE ISSUE');

}

});

return false;

}//end size

else

{

imgclean.replaceWith( imgclean = imgclean.clone( true ) );//Its for reset the value of file type

alert('Sorry File size exceeding from 1 Mb');

}

}//end FILETYPE

else

{

imgclean.replaceWith( imgclean = imgclean.clone( true ) );

alert('Sorry Only you can uplaod JPEG|JPG|PNG|GIF file type ');

}

}//end filecount

else

{ imgclean.replaceWith( imgclean = imgclean.clone( true ) );

alert('You Can not Upload more than 6 Photos');

}

}

这是PHP代码:

$filetype = array('jpeg','jpg','png','gif','PNG','JPEG','JPG');

foreach ($_FILES as $key )

{

$name =time().$key['name'];

$path='local_cdn/'.$name;

$file_ext = pathinfo($name, PATHINFO_EXTENSION);

if(in_array(strtolower($file_ext), $filetype))

{

if($key['name']<1000000)

{

@move_uploaded_file($key['tmp_name'],$path);

echo $name;

}

else

{

echo "FILE_SIZE_ERROR";

}

}

else

{

echo "FILE_TYPE_ERROR";

}// Its simple code.Its not with proper validation.

这里上传和预览部分已完成.现在如果你想从页面和文件夹中删除和删除图像,那么代码就在这里删除.Ajax部分:

function removeit (arg) {

var id = arg;

// GET FILE VALUE

var fname = $('#filename').val();

var fcnt = $('#filecount').val();

// GET FILE VALUE

$('#img_'+id).remove();

$('#rmv_'+id).remove();

$('#img_'+id).css('display','none');

var dname = $('#name_'+id).val();

fcnt = parseInt(fcnt)-1;

$('#filecount').val(fcnt);

var fname = fname.replace(dname, "");

var fname = fname.replace(",,", "");

$('#filename').val(fname);

$.ajax({

url: 'delete.php',

type: 'POST',

data:{'name':dname},

success:function(a){

console.log(a);

}

});

}

这是PHP部分(delete.php):

$path='local_cdn/'.$_POST['name'];

if(@unlink($path))

{

echo "Success";

}

else

{

echo "Failed";

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值