• We just launched and are currently in beta. Join us as we build and grow the community.

Multiple Image Preview using jQuery

sersha

Codebase Quality Auditor
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
165
Likes
114
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial will teach you how to preview multiple images before uploading using jQuery.

Note: jQuery used in this tutorial is hosted, so, you might need internet connection for it to work.

index.html

This contains our input file that we have customize and our preview area.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Multiple Image Preview using jQuery</

    title

    >
  5. <script

    src

    =

    "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"

    ></

    script

    >
  6. <link

    href

    =

    "https://fonts.googleapis.com/css?family=Open+Sans"

    rel

    =

    "stylesheet"

    >
  7. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"

    rel

    =

    "stylesheet"

    >
  8. <style

    >
  9. body{
  10. font-family: 'Open Sans', sans-serif;
  11. }
  12. .button {
  13. background-color: #4CAF50;
  14. border: none;
  15. color: white;
  16. padding: 16px 32px;
  17. text-align: center;
  18. text-decoration: none;
  19. font-size: 16px;
  20. margin: 4px 2px;
  21. cursor:pointer;
  22. font-family: 'Open Sans', sans-serif;
  23. }
  24. .main{
  25. margin-top:50px;
  26. }
  27. .hidden{
  28. display:none;
  29. }
  30. .h10{
  31. height:10px;
  32. }
  33. #filetext{
  34. margin-left:10px;
  35. }
  36. .title{
  37. font-size:25px;
  38. }
  39. .container {
  40. margin : 50px auto 0 auto;
  41. width:50%;
  42. }
  43. #filebutton{
  44. margin : 50px auto 0 auto;
  45. }
  46. #preview{
  47. margin-top:50px;
  48. }
  49. </

    style

    >
  50. </

    head

    >
  51. <body

    >
  52. <div

    id

    =

    "container"

    >
  53. <div

    class

    =

    "main"

    >
  54. <span

    class

    =

    "title"

    ><strong

    ><center

    >

    Multiple Image Preview using jQuery</

    center

    ></

    strong

    ></

    span

    >
  55. <span

    ><center

    >

    by <a

    href

    =

    "https://www.sourcecodester.com/user/224918/track"

    >

    nurhodelta_17</

    a

    ></

    center

    ></

    span

    >
  56. <div

    class

    =

    "h10"

    ></

    div

    >
  57. <input

    type

    =

    "file"

    name

    =

    "file"

    id

    =

    "file"

    class

    =

    "hidden"

    multiple>
  58. <center

    ><button

    type

    =

    "button"

    class

    =

    "button"

    id

    =

    "filebutton"

    ><i

    class

    =

    "fa fa-upload"

    ></

    i

    ><span

    id

    =

    "filetext"

    >

    Select File</

    span

    ></

    button

    ></

    center

    >
  59. <div

    id

    =

    "preview"

    ></

    div

    >
  60. </

    div

    >
  61. </

    div

    >
  62. <script

    src

    =

    "custom.js"

    ></

    script

    >
  63. </

    body

    >
  64. </

    html

    >

custom.js

This contains our jQuery for the input file custom and image preview.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '#filebutton'

    )

    .click

    (

    function

    (

    )

    {
  3. $(

    '#file'

    )

    .click

    (

    )

    ;
  4. }

    )

    ;

  5. $(

    '#file'

    )

    .change

    (

    function

    (

    )

    {

  6. var

    name =

    $(

    this

    )

    .val

    (

    )

    .split

    (

    '\\

    '

    )

    .pop

    (

    )

    ;
  7. var

    file =

    $(

    this

    )

    [

    0

    ]

    .files

    ;
  8. if

    (

    name !=

    ''

    )

    {
  9. if

    (

    file.length

    >=

    2

    )

    {
  10. $(

    '#filetext'

    )

    .html

    (

    file.length

    +

    ' files ready to upload'

    )

    ;
  11. }
  12. else

    {
  13. $(

    '#filetext'

    )

    .html

    (

    name)

    ;
  14. }
  15. }
  16. }

    )

    ;

  17. $(

    '#file'

    )

    .on

    (

    "change"

    ,

    previewImages)

    ;
  18. }

    )

    ;

  19. function

    previewImages(

    )

    {

  20. var

    $preview =

    $(

    '#preview'

    )

    .empty

    (

    )

    ;
  21. if

    (

    this

    .files

    )

    $.each

    (

    this

    .files

    ,

    readAndPreview)

    ;

  22. function

    readAndPreview(

    i,

    file)

    {

  23. if

    (

    !/

    \.(

    jpe?

    g|

    png|

    gif)

    $/

    i.test

    (

    file.name

    )

    )

    {
  24. return

    alert(

    file.name

    +

    " is not an image"

    )

    ;
  25. }

    // else...

  26. var

    reader =

    new

    FileReader(

    )

    ;

  27. $(

    reader)

    .on

    (

    "load"

    ,

    function

    (

    )

    {
  28. $preview.append

    (

    $(

    "<img>"

    ,

    {

    src:

    this

    .result

    ,

    height:

    200

    }

    )

    )

    ;
  29. }

    )

    ;

  30. reader.readAsDataURL

    (

    file)

    ;

  31. }

  32. }


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top