javascript - Upload image immediately on select -
here @ last question, no 1 can give me right solution. thank process upload image.
now want upload image after selection input type file.
how script.
html: (here 888 dynamic id php)
<form class="upload_reply" id="888'" method="post" enctype="multipart/form-data"> <label for="file">filename (max 200 kb) :</label> <input type="file" name="file" class="repfile" id="888" value="" /> </form>
script:
$(document).on('change','.repfile',function (){ previewpic(this); }); function previewpic(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $("#preview_rep"+ input.id).attr('src', e.target.result); $("#output_rep"+ input.id).show(); var str = e.target.result; var id = input.id; uploadfile(str, id); }; reader.readasdataurl(input.files[0]); } } function uploadfile(str, id) { str = ??? var eid = id; $('.loading').show(); $.ajax({ type:"post", url:"../upload.php", data: new formdata(this), contenttype: false, cache: false, processdata:false, success: function(response){ $('#img'+ eid).attr('value', response); $('.loading').hide(); }, }); return false; }
i recommend use jquery third party reliable stable plugin rather own custom code
with of plugin can
show progress of file uploaded
verify if right file selected
preview image
change preview
and many more option
some of best plugin , usable plugin
i myself use uploadify, work me.
$(document).ready(function (){ $('#repfile').uploadify({ 'uploader' : '/uploadify/uploadify.swf', 'script' : '/uploadify/uploadify.php', 'cancelimg' : '/uploadify/cancel.png', 'folder' : '/uploads', 'auto' : true }); });
hope answer helpful
other can visit here - upload files ajax
Comments
Post a Comment