Last active
April 11, 2019 15:37
-
-
Save digamber89/e4fbe86d2926e49e1a815d115568533c to your computer and use it in GitHub Desktop.
Multi Image Uploader Using - jQuery Dependent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="digthis-multi-upload-form" action="" method="post" enctype="multipart/form-data"> | |
<div> | |
<label for="title">Title</label> | |
<input type="text" id="title" name="title" required> | |
</div> | |
<div> | |
<label for="content">Content</label> | |
<textarea id="content" name="content" required></textarea> | |
</div> | |
<div class="digthis-multi-file-uploader" style="margin-top:20px"> | |
<div class="preview-section"></div> | |
<input id="imgInp" name="issue-images[]" type="file" multiple> | |
</div> | |
<div style="margin-top:20px"> | |
<input type="submit" value="submit"> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
var previewFileImages = { | |
init: function () { | |
this.cacheDOM(); | |
this.eventListeners(); | |
this.defaultActions(); | |
}, | |
cacheDOM: function () { | |
this.$issueForm = $('.digthis-multi-upload-form'); | |
this.$fileUploadContainer = $('.digthis-multi-file-uploader'); | |
this.$fileInput = this.$fileUploadContainer.find('input[type="file"]'); | |
this.$imagePreviewSection = this.$fileUploadContainer.find('.preview-section'); | |
this.uploadedFiles = []; | |
}, | |
defaultActions: function(){ | |
this.$fileInput[0].value = ''; | |
}, | |
eventListeners: function () { | |
this.$issueForm.on('submit', this.submitForm.bind(this)); | |
this.$fileInput.on('change', this.readURL.bind(this)); | |
this.$imagePreviewSection.on('click', 'img', this.removeImages.bind(this)); | |
}, | |
submitForm: function (e) { | |
e.preventDefault(); | |
var $el = $(e.currentTarget); | |
//for wordpress | |
//var nonce = $el.find('input[name="digthis_multiple_upload_form"]').val(); | |
var title = $el.find('input[name="title"]').val(); | |
var content = $el.find('input[name="content"]').val(); | |
var noOfFiles = this.uploadedFiles.length; | |
//https://developer.mozilla.org/en-US/docs/Web/API/FormData/append | |
var fd = new FormData(); | |
fd.set('digthis_multiple_upload_form', nonce); | |
fd.set('title', title); | |
fd.set('content', content); | |
for (i = 0; i < noOfFiles; i++) { | |
fd.append('issue_images[]', this.uploadedFiles[i]); | |
} | |
$.ajax({ | |
url: ajax_url,//assumes action | |
data: fd, | |
processData: false, | |
contentType: false, | |
type: 'POST', | |
success: function (data) { | |
alert('submitted'); | |
} | |
}); | |
}, | |
removeImages: function (e) { | |
var $el = $(e.currentTarget); | |
var index = $el.data('id'); | |
this.$fileInput[0].files[i] = ''; | |
var newFileList = Array.from(this.$fileInput[0].files); | |
newFileList.splice(index, 1); | |
this.uploadedFiles = newFileList; | |
$el.remove(); | |
}, | |
fileOnLoad: function (index) { | |
var i = index; | |
e = this.event; | |
return function (e) { | |
$imagePreviewSection = $('.preview-section'); | |
$imagePreviewSection.append('<img src="' + e.target.result + '" width="150" height="150" data-id="' + i + '">') | |
}; | |
}, | |
readURL: function (e) { | |
var input = e.currentTarget; | |
if (input.files) { | |
var count = input.files.length; | |
for (i = 0; i < count; i++) { | |
var uploaded_file = input.files[i]; | |
var reader = new FileReader(); | |
reader.addEventListener('load', this.fileOnLoad(i)); | |
reader.readAsDataURL(uploaded_file); | |
} | |
} | |
this.$imagePreviewSection.html(''); | |
this.uploadedFiles = input.files; | |
} | |
}; | |
$(function(){ | |
previewFileImages.init(); | |
}); | |
})(jQuery); |
Author
digamber89
commented
Apr 11, 2019
- Has a jQuery Dependency if the code wasn't clear enough.
- Files can only be submitted via ajax
- Styling not included as was originally applied for a WP project
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment