In the survey I’m currently working on, we have our users upload a receipt. I’d like to have that receipt in an iframe later on in the survey so they can reference it for other questions. However, when I try to set up the iframe using embedded data (<iframe src="${e://Field/receipt}"></iframe>), it loads a blank frame and opens the file twice in new tabs.
Is it possible to load it in the survey itself, rather than a tab? I feel like I must be missing something obvious.
Page 1 / 1
Hi, the below is working for me to view an image file within an iframe that was uploaded using the file upload question.
First, create an Embedded Data field called "FileURL" and put it after the Block that contains the File Upload question in the Survey Flow. Use Piped text to set this field with the File URL, like in the below:
Next, in a Block that comes after the Embedded Data element, create a Text/Graphic question and use the Rich Content Editor's HTML/Source view "<>" to update the Question Text with the below:
Thanks for your answer. While both of your suggestions work for image files, they don’t work for PDFs, and our users regularly submit receipts in both formats. I’ve also tested whether <embed> or <object> will work, but no luck. I’m wondering if this is just a Qualtrics quirk, since I haven’t had any issues with using iframe/embed/object to display PDFs in external sites.
I see what you mean. In this stackoverflow thread, someone suggests that the download happens because "The server sends the data with a HTTP header inline-disposition that causes a download. You can strip this header via declarativeNetRequest or webRequest."
Also in that thread, the original poster patchie found a solution which involved using JS to convert the document to base 64 (discussed here) and used jQuery to insert it into an embed. I have adapted it so that the base 64 data will be used if the file name contains "pdf" and the piped URL for all other file names. The iframe is inserted after the question text using JS.
To give it a try, create the Embedded Data fields of "FileURL" and "FileName" and put them after the block that contains the file upload question . Set FileURL with the uploaded file URL and FileName with the file's name using piped text. Also include the CSS from my other post.
In a Text/Graphic question that comes in a block after the Embedded Data element, add the below to the question's JavaScript:
function base64Encode(str) { var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var out = "", i = 0, len = str.length, c1, c2, c3; while (i < len) { c1 = str.charCodeAt(i++) & 0xff; if (i == len) { out += CHARS.charAt(c1 >> 2); out += CHARS.charAt((c1 & 0x3) << 4); out += "=="; break; } c2 = str.charCodeAt(i++); if (i == len) { out += CHARS.charAt(c1 >> 2); out += CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); out += CHARS.charAt((c2 & 0xF) << 2); out += "="; break; } c3 = str.charCodeAt(i++); out += CHARS.charAt(c1 >> 2); out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)); out += CHARS.charAt(c3 & 0x3F); } return out; }
function getBinary(file){ var xhr = new XMLHttpRequest(); xhr.open("GET", file, false); xhr.overrideMimeType("text/plain; charset=x-user-defined"); xhr.send(null); return xhr.responseText; }
var file = "${e://Field/FileURL}"; var b = getBinary(file); var b64 = base64Encode(b); console.log(b64);