Advertisement
joelleo

java to control picture

Sep 30th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var abc = 0; // Declaring and defining global increement variable.
  2. $(document).ready(function() {
  3. // To add new input file field dynamically, on click of "Add More Files" button below function will be executed.
  4. $('#add_more').click(function() {
  5. $(this).before($("<div/>", {
  6. id: 'filediv'
  7. }).fadeIn('slow').append($("<input/>", {
  8. name: 'file[]',
  9. type: 'file',
  10. id: 'file'
  11. }), $("<br/><br/>")));
  12. });
  13. // Following function will executes on change event of file input to select different file.
  14. $('body').on('change', '#file', function() {
  15. if (this.files && this.files[0]) {
  16. abc += 1; // Incrementing global variable by 1.
  17. var z = abc - 1;
  18. var x = $(this).parent().find('#previewimg' + z).remove();
  19. $(this).before("<div id='abcd" + abc + "' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
  20. var reader = new FileReader();
  21. reader.onload = imageIsLoaded;
  22. reader.readAsDataURL(this.files[0]);
  23. $(this).hide();
  24. $("#abcd" + abc).append($("<img/>", {
  25. id: 'img',
  26. src: 'x.png',
  27. alt: 'delete'
  28. }).click(function() {
  29. $(this).parent().parent().remove();
  30. }));
  31. }
  32. });
  33. // To preview image
  34. function imageIsLoaded(e) {
  35. $('#previewimg' + abc).attr('src', e.target.result);
  36. };
  37. $('#upload').click(function(e) {
  38. var name = $(":file").val();
  39. if (!name) {
  40. alert("First Image Must Be Selected");
  41. e.preventDefault();
  42. }
  43. });
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement