Welcome, Guest
Username: Password: Remember me

TOPIC: Show Results of File Upload Question in Subsequent Question

Show Results of File Upload Question in Subsequent Question 3 months 1 week ago #103347

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Silver Donor
  • Posts: 182
  • Thank you received: 5
  • Karma: 5
Hi all,

Using Expression Manager, I can see the results of the file upload question in the survey.
Sorry for reporting - I originally posted in installation issues.

{fileupload.NAOK} =
[{"title":"Starry","size":"0.815","name":"star.gif","filename":"futmp_hai8nxjvd72den4_gif","ext":"gif"}]

Two questions.

1. If I want to show this result in a subsequent question, I think I would need to parse this array, and utilize the URL of the image to present it back to the user.



2. I've read some posts indicating that downloading the uploaded file is problematic. One can manually browse the responses of a live survey and download the image easily enough, but downloading the file directly from the server is a little problematic. Star.gif is chnanged into into fu_456yddyvpxdxzq8 and stored in the survey's directory without a file extension.

If one wanted to use the image in an email template for example, this is problematic.

Perhaps a javascript function could be written to "store the file twice - once as it is for use as LimeSurvey intends it, and again in a secondary location in its orginal form. Perhaps it would be easiest to include a non-native fileupload function for this purpose. Perhaps there is another solution.

Interested in your thoughts on 1 and 2.
Cheers,
David
The administrator has disabled public write access.

Show Results of File Upload Question in Subsequent Question 3 months 1 week ago #103376

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Regarding #1, as far as I know there is no way to parse that array with Expression Manager but you can with JavaScript.

Add the following to the end of template.js:
function displayUploadedImage(results, imgNumber) {
 
	if($.isArray(results)) { // Only if there is an upload result
 
		var resultsArr = new Array();
		resultsArr = results;
		var imgArr = resultsArr[imgNumber-1];
 
		if(imgArr) { // Only if this image number exists
 
			var imgExtLC = imgArr['ext'].toLowerCase();
			if(imgExtLC == 'gif' || imgExtLC == 'png' || imgExtLC == 'jpg' || imgExtLC == 'jpeg' || imgExtLC == 'tif' || imgExtLC == 'tiff' || imgExtLC == 'bmp') { // Only the file is an image
 
				var surveyRoot = location.pathname.split('index.php')[0];
				var fieldNames = $( 'input#fieldnames' ).attr('value');
				var tmp = fieldNames.split('X');
				var sID = tmp[0];
				var imgFilename = imgArr['filename'];
				document.write('<img src="'+surveyRoot+'index.php/uploader/index/sid/'+sID+'/filegetcontents/'+imgFilename+'" class="pipedImage" />');
			}
			else {
				displayImageError();
			}
		}
		else {
			displayImageError();
		}
 
	}
}
 
function displayImageError() {
	document.write('<span style="color:red">No image found</span>');
}

Then add this script to the question text source where you want the image to appear.
Note: "uploadQuestionCode" is your upload question code and "1" is the image number from that question (in case you allow multiple uploads in the question):
<script type="text/javascript" charset="utf-8">	 displayUploadedImage({uploadQuestionCode}, 1); </script>

Here is a working survey and template (install the template first):

File Attachment:

File Name: Demo_Display_Uploaded_Images.zip
File Size: 117 KB

File Attachment:

File Name: Demo_Display_Uploaded_Images.lss
File Size: 15 KB


.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 months 1 week ago by tpartner.
The administrator has disabled public write access.

Show Results of File Upload Question in Subsequent Question 3 months 5 days ago #103488

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Silver Donor
  • Posts: 182
  • Thank you received: 5
  • Karma: 5
TParter,
Thank you tremendously for this. I haven't yet a chance to implement this solution. I was able to use substr functions to identify the parts of the array that described the image, but you are right, I wasn't able to show the image. Thank you!

This function should allow one to use the uploaded image say in an email template too right?
- Nebraska
The administrator has disabled public write access.

Show Results of File Upload Question in Subsequent Question 3 months 5 days ago #103489

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
Sadly, I don't think this will work in emails. You need to have a valid session to view the image. (either taking the survey or logged into the backend)

.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Show Results of File Upload Question in Subsequent Question 3 months 5 days ago #103493

  • dweisser
  • dweisser's Avatar
  • OFFLINE
  • Silver Donor
  • Posts: 182
  • Thank you received: 5
  • Karma: 5
Understood.
You can reconstruct the file's name and the url of the image using substr and other string manipulation with Expression Manager. What you can't do is "somehow" append the file extension onto the converted file once uploaded.

Is there a way to "branch" the question upload function such that two images are stored? Trigger a function that uploads the file such as is done for the template editor - before the function that stores it in the survey's answer table?
The administrator has disabled public write access.

Show Results of File Upload Question in Subsequent Question 3 months 5 days ago #103494

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
What you can't do is "somehow" append the file extension onto the converted file once uploaded.
Correct, the file is (sort of) encrypted when stored.

Is there a way to "branch" the question upload function such that two images are stored?
Not with JavaScript. It may be possible by hacking the core code but would introduce some security issues - the files would be accessible to everyone.

.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 months 5 days ago by tpartner.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.112 seconds
Donation Image