HOWTO: Attach images to a Forms 2.0 form post | Community
Skip to main content
SanfordWhiteman
Level 10
April 10, 2015
Question

HOWTO: Attach images to a Forms 2.0 form post

  • April 10, 2015
  • 8 replies
  • 10378 views
Not a question, but an answer!

In a couple of Community threads I've remarked that -- despite conventional wisdom -- it is possible to attach images to Marketo forms (both LP-hosted and embedded) and save the image data in your Marketo database.

To prove I wasn't blowing smoke, here's a demo.

This form populates the built-in MarketoSocialLinkedInProfileURL field with an image of the lead's choosing; you can use any text field you want. In the demo, the file size is limited to 64K: since the image is exported as a maximum 200 x 200 low-quality JPEG, that'll do fine, but the larger you set the MAX_WIDTH and MAX_HEIGHT, the larger the file will naturally be.

At this point in time I would recommend this technique only for obviously small (in dimensions) images such as avatars, small headshots, or logos... though I suppose if you know people will be uploading 2-color scans, for example, you could go bigger, at your own risk.   (If you've worked at all with web images, you know there is no hard-and-fast rule because I could make a "giant" 5000 x 5000 JPG that's only a couple K on disk -- the size on the wire depends on complexity and compressibility.)

I hope at least some of you are impressed so far! Here are the caveats/details:
  • Does not work in IE 8 or 9.  I'm sympathetic to people who have to support old browsers -- we still expect and support IE 8 for our web app -- but sometimes you have to build something cool, no matter what.  If you aren't comfortable pushing people to use IE 10+, Chrome, Safari, or Firefox, you will have to consider this technique a "progressive enhancement" and apply it appropriately (for example, let people use a default avatar if their browser doesn't support attaching an avatar to the form post).  You could get it working in old IE using Silverlight, Flash, or Java... writing that code is up to you. :)
  • The Marketo app will not display the image as an picture but rather as a URL -- technically, a data URI, which is just an agglomeration of bytes into something that looks like a URL  -- which you need to copy-paste into another browser tab to view. (You could build a bookmarklet to render it into the page, most likely, but I'll leave that up to you.)  
  • Aside from the default Marketo UI, all other ways of reading data from Marketo, including the Forms APIs and the REST/SOAP APIs, and even SFDC, are capable of displaying the picture in your browser.  You could, for example, create a VisualForce page that displays the image in SFDC, or read the field using REST and convert it to a standalone image to use anywhere you want.
  • The technical implementation is all there in the JavaScript, so how awesome or horrifiying it is probably depends on your love of code.
  • Hope you like it!

8 replies

Justin_Norris1
Level 10
April 10, 2015
Thanks for this Sandy. That is pretty cool!

Now you're going to make me struggle with my shoddy JS knowledge as I poke through to see how you've done it ;)
SanfordWhiteman
Level 10
April 10, 2015
Was hoping you'd check it out, Justin.

The basic concept was to phrase the question like so: "Can Marketo store data that can be displayed as an image?" (As opposed to the more common question, "Can you upload an image with a form?")   Once phrased that way, it was easy to test that a text field can indeed hold a Base64-encoded data-URI of reasonable length.  Then what remained was to figure out how to get such data into a form post.  Since we run an image hosting service that part was somewhat easy. :)

Just hit me up here or offline with any questions!
Jep_Castelein2
Level 10
April 28, 2015
Very nice solution, thanks for sharing. 
Neil_Robertson6
Level 4
October 6, 2015

Thanks Sandford - Great JS but only downside I can see is the size limit of a small 64k!

SanfordWhiteman
Level 10
October 6, 2015

You can actually split the files across multiple textareas to get more space (Base64 is easily splittable and joinable). But that can get unmanageable once you get past 3-4 fields, as you can imagine.  I've since found a third-party upload/storage provider that really Gets It (tm) about integrating with independent forms (not just Marketo) and I've been recommending them to my clients.  PM me and I'll tell you about them if you want.

October 8, 2015

Interesting, I nice it seems this can turn in to different options

January 20, 2016

Amazing feature! Will it be possible to upload files such as pdf or docx some day?

Regards

SanfordWhiteman
Level 10
January 21, 2016

Amazing feature! Will it be possible to upload files such as pdf or docx some day?

Either one would work if they're small enough.  The irony (if you can call it that given the potential size of images in general) is that because of JPEG compression, outputting a compressed image with fixed dimensions is very likely to stay under the limit, whereas unless DOCX/PDF comes from a template with a set # of pages/fields it could get very huge, very quickly.

These days I am more likely to suggest Uploadcare as discussed and demoed here: Re: File Upload Field

May 3, 2017

Hi Sanford,

I've been reading up on the form you created here MktoForms2 :: Uploadcare - I just wanted to confirm what is the field name of the hidden field you created in the JS and what other actions did you do on the marketo database side to populate the URL into the hidden field?

I've tried recreating the the form you created (same HTML and JS just switched over to the form reference to what my own form as well as using my uploadcare public key) I even named my string field RFPURL however it still does not populate a URL on marketo after submission.

Not sure what I'm doing wrong as my javascript skills are very beginner level.

Please let me know your thoughts, happy to provide more information if necessary

Thanks!

Rafael

SanfordWhiteman
Level 10
May 3, 2017

Hi Raf,

There's no other work done on the Marketo side, but you need to have the corresponding custom (string) field in Marketo of course.

You'd have to point me to your URL for me to troubleshoot further.

May 3, 2017

Hi Sanford,

Edit fiddle - JSFiddle

Please let me know your thoughts

Raf

Carrie_Thomas1
Level 2
July 5, 2017

Can you upload other file types such as a word doc? 

SanfordWhiteman
Level 10
July 5, 2017

Sure, UploadCare supports all binary file types.

Hana_AlBarazi
Level 2
May 4, 2018

Hi @Sanford Whiteman, this is really cool! Thanks for sharing. I am running into some major confusion as to how I can render the URI into an image, though. When I copy and paste into another tab in my Chrome browser, it doesn't load appropriately. Perhaps I'm too new to these things to understand the alternate ways I can access the data that you described, and I'm wondering if you can you point me to a resource that gives a basic explanation of how to access it in those ways, step by step?

Thanks for your help!

SanfordWhiteman
Level 10
May 4, 2018

You'll have to tell me more (a lot more!) about how you're trying to use the URI.

Here's a field I just created on a lead using that form and embedded like so, directly into this Community post:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAYABgDAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD86fgz8D/HXx617VfC/wAPIbK41XStIl1j7Nc3IgNzHHLFEY42YbPMLTLjeyrgHLDgEA5rxh4J8X/D7XZvDPjfw3qOh6pb8va31u0TlckB1yPmQ4OHXKnqCRQBi0AfZH/BLD/k4PxB/wBibd/+l1lQB9+/tC/8M+/8ILJ/w0V/wj39gfP5P9qf67zNvzfZdn7/AM3bn/UfPjOKAPxv+MP/AAqL/hOr3/hSP/CQ/wDCL/8ALH+3PL87zNzbvL2c+Vjbs3/Pj73NAFr4M/HDx38Bde1XxR8PJrK31XVdJl0f7Tc2wnNtHJLFKZI1Y7PMDQrjerLgnKnggA5rxh428XfEHXZvE3jfxJqOuapccPdX1w0rhckhFyflQZOFXCjoABQBi0Af/9k=" />

9k=

Hana_AlBarazi
Level 2
May 4, 2018

Hi Sanford - thanks for the quick response. I'm trying to set up a form on a Marketo landing page where people can upload a headshot that we will place on our website. I'm just having trouble accessing the actual image after successfully uploading it using the script you provided. The field is being loaded correctly (I think) with the URI (pasted below), but when I paste it into my Chrome and Safari browsers, I get an error message, "The webpage at, [URI LINK] might be temporarily down or it may have moved permanently to a new web address. ERR_INVALID_URL".

  1. <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBE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80PKnBScZohxGRQiFFMkpkcpOKFLZDexoVFeUpbQAKPSnbJGK5SOXqOtGAEncVaRA1AAk12lIA32r3YDNDpXlNe/SvOtAHPQUYy2pawCM79vOu9uSuot0sxozkuQ4EtMoK1q8gKyxxu4lC+XBcdtwpYZTysNBWAkeZPmev8AoMVdPGrU5tFtVYoiySr/AKpBOVKwdgO4Gax7qRpFylrQy7ykrJy6ncE9T1/eszqWb1XbG9gtBpuJ0x1Hdym4akX955DI5k53Qg+vp/vU+020xIZDYbyzMODtnYJOBv8AU7f2GbQPCCdquYllrKgo/OgfL7GtSaK+zXMjRWlPcnKjBwd87f70r6ld08ige/mlmS7256NJU1AjguSFBOSnZKe5PoMdBjpnvTLLnFmR4CVoLaO7iQTjuo7YH0H61rrUH2fZcJas2/xAvIStIzgHbfHSu7F9kO3XFgSbmkjnThQUMY/v+tQMgRRiOJ4WVLVcrc8pLSJTZUcHCkYzv59R+dTaPbzKiiVGaUVIbAcbIyUjGNsbKGOvfGe9Svil9k+7aUWu8aVDkqK3lTjadlAentUE0Rf3LTczbLqtJa/6RUvIWyScELHUpOeuetdY4FRkifF+YJ2sDEyxzhPtb7iSysEhOVcoPTYdU75x+VaX0FqRjWVsU2SlFyjDK2ubPOnsQe4ONj6EHcGqtYsf3iMbtEbw9Gd8KQ3zhQII5gM9z1IPfc9eiuJJmaQfiatsRH3RKv8AEIJT4Ks9FduUnAV2GxHQZt4078Z4c1LczGZks91b7jZT8wrg5" /> 

I also tried to embed the photo below, but this is the icon I get, in the place of where the photo should be.

Also, here's my fiddle.

So perhaps it's my limited knowledge of what I'm actually doing that is preventing me from understanding what the problem is, but I thought I followed your instructions pretty closely.

Thanks for the guidance, it is much appreciated.