Thursday, 8 December 2011

Gmail, not sending attachments

google's GMail has a nice way unsecured loans of allowing you to add multiple attachments to an email. Rather than showing you 10 file upload boxes at once, the user attaches a file, you can click a button to add another attachment. I had a client request this functionality in an app I am building for them today, and it was pretty easy to implement.
Here's the code:
<input type="file" name="attachment" id="attachment" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<div id="moreUploads"></div>
<div id="moreUploadsLink" style="display:none;"><a href="javascript:addFileInput();">Attach another File</a></div>
And now here's the javascript bad credit loans  function to add another input box:
var upload_number = 2;
function addFileInput() {
  var d = document.createElement("div");
  var file = document.createElement("input");
  file.setAttribute("type", "file");
  file.setAttribute("name", "attachment"+upload_number);
  d.appendChild(file);
  document.getElementById("moreUploads").appendChild(d);
  upload_number++;
}
Google also does something else that is pretty slick in Gmail with their attachment uploads - they appear to upload the file using AJAX. So you can type your email while your files are uploading. Perhaps I'll look into doing that in a future blog entry.

No comments:

Post a Comment