You are viewing limited content. For full access, please sign in.

Question

Question

Java Script Preview Image while completing the form

asked on July 26, 2017

I have created a basic expense reimbursement form. We would like a preview of the image be on the form while the employee is completing it, and the preview be available on tasks for the manager approval. I found the JS below on another post, but it is not working on my form. Can anyone tell me what is incorrect for my form>

Expense Form.jpg
Expense Java.jpg
Expense Form.jpg (30.68 KB)
Expense Java.jpg (55.29 KB)
0 0

Answer

SELECTED ANSWER
replied on July 26, 2017 Show version history

That bit of Javascript works fine, you also have to put a piece of Javascript in the go behind file as well.  that's the second part. 

https://answers.laserfiche.com/questions/90599/Viewing-a-Form-attachment-in-LF-client

EDIT. I think I used the revised code in this post actually https://answers.laserfiche.com/questions/112408/File-Upload-Preview 

My current code for my forms looks like this. I had to add a custom HTML class <div class="gallery"></div>

  var iml = ['jpg','jpeg','gif','png']; 
    $('.files tbody tr').each(function() { 
      if ($(this).find('a').length && $.inArray($(this).find('a').attr('title').match(/\.(\w{3,4})$/)[1],iml)>-1) { 
        var ims = $(this).find('a').attr('href'), 
            imr = $('<img style="width:300px;" />'); 
        imr.attr('src',ims); 
        $('<tr><td colspan=3>'+imr.prop('outerHTML')+'</td></tr>').insertAfter($(this).find('a').closest('tr')); 
      } 
    });
	
  	var imgsrc= $(this).find("a").attr('href');
  	
 	//image upload preview
     $(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                  	//append HTML and limit size of image preview
                    $($.parseHTML('<img style="width:300px;">')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
                }

                reader.readAsDataURL(input.files[i]);
            }
        }

    };

        $('#Field1').on('change', function() {
            imagesPreview(this, 'div.gallery');
        });
    });	

 

1 0

Replies

replied on July 28, 2017

This is not working as i was thinking. I was wanting as they were filling out the form, once they upload the file for a preview to be show as they complete the rest of the form.

 

My main goal is for once the employee submits the expense reimbursement form, that when the manager goes to approve the form, they do not have to click to see the attachment, rather the image is displayed on screen. 

0 0
replied on July 28, 2017 Show version history

If you want it to work as they are filling out the form, you'll need to detect the addition of each file and update your preview when a file is attached.

The following worked for me:

First, I created a custom HTML element on the form with just the following html

<div id="imageContainer"></div>

Then, I got the identifier for the Upload field (in my test form it was #q26)

Next, I bound a DOM node insertion handler to the file list/table to detect when it actually got the new entry (the a element with the file reference).

I used this approach because there is a slight delay between the input change event and the data actually becoming available, whereas the DOM event is precise.

  $("#q26 .files").bind("DOMNodeInserted DOMNodeRemoved",function(){
    // wipe the slate clean to prevent duplicated images
    $('#imageContainer img').remove();
    
    // Set delay to better support file removal
    setTimeout(function(){
      // grab each file entry
      $('#q26 .file .ellipsis').each(function(){
        // get file extension
        var extension = $(this).attr('title').split('.').pop().toUpperCase();

        // if valid image type
        if($.inArray(extension,['JPEG','JPG','GIF','PNG']) != -1){
          // append an img element for each uploaded image
          $('#imageContainer').append('<img src="'+ $(this).attr('href') +'" width="300">');
        }
      });
    },50);
  });

Only monitoring DOM insertions on the upload field is important because monitoring the entire document would generate a LOT of events.

If you strip out the inner ".each" portion and run that on document ready, that should get the images to display for the next reviewer.

NOTE: The setTimeout is there because of a brief delay on the DOM element removal. With this in place it should update successfully when files are added as well as removed.

1 0
replied on August 1, 2017

My code should do that for you. You'll need to create a div called gallery and style it how you want. I have live previews throughout my current form as different uploads in each section

1 0
replied on September 13, 2017

I have observed that the photos taken from my iphone don't display in the the order in which they were taken. For example if I take pictures of pages with 1, 2, and 3 written on them, they display on the form in a computer web browser as 2, 1, 3.

Note that I am only using the first section of code in my custom javascript for the form, as follows:

 

$(document).ready(function () {

    var iml = ['jpg','jpeg','gif','png'];

    $('.files tbody tr').each(function() {

      if ($(this).find('a').length && $.inArray($(this).find('a').attr('title').match(/\.(\w{3,4})$/)[1],iml)>-1) {

        var ims = $(this).find('a').attr('href'),

            imr = $('<img style="width:550px;" />');

        imr.attr('src',ims);

        $('<tr><td colspan=3>'+imr.prop('outerHTML')+'</td></tr>').insertAfter($(this).find('a').closest('tr'));

      }

    });

});

 

Does anyone know how to tweak the code to display them in order?

Thanks,

Alon

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.