Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. const img_max = 10;
  2. let reader = new FileReader();
  3. let files = []; // user photos stored here
  4. reader.onload = function (e) {
  5. $('#img_'+files.length).attr('src', e.target.result);
  6. $('#img_container_'+files.length).css('display', 'inline-block');
  7. };
  8.  
  9. function readURL(input) {
  10. if (input.files && input.files[0]) {
  11. reader.readAsDataURL(input.files[0]);
  12. files.push(input.files[0]);
  13. }
  14. }
  15.  
  16. $("#img_input").change(function() {
  17. if (files.length < img_max)
  18. readURL(this);
  19. });
  20.  
  21. function deleteImg(index)
  22. {
  23. let cur, next;
  24. for (let c = index; c < img_max; c++)
  25. {
  26. cur = $('#img_'+c);
  27. next = $('#img_'+(c+1)).attr('src');
  28. if (next == null) {
  29. cur.attr('src', null);
  30. $('#img_container_'+c).hide();
  31. break;
  32. }
  33. cur.attr('src', next);
  34. }
  35. files.splice(index, 1);
  36. }
  37.  
  38. $('#form').submit(function (e)
  39. {
  40. let input = ($('#img_input'))[0];
  41. for (let c = 0; c < files.length; c++)
  42. input.files[c] = files[c];
  43. return true;
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement