Monday 22 July 2013

Checking A File's Upload Size In The Browser

This really just a quick note so I don't lose the code snippet I just used...

I'm working on a form which allows users to upload a file, but we want to stop them uploading giant things. Ultimately the server's maximum upload filesize will stop anything too huge getting through, but it's nice to remind people of the limit before they bother uploading.

This is a great little snippet to do that in current versions of Chrome, Firefox and Safari.

$('#contact_file').bind('change', function() { 
  filebytes = this.files[0].size;
  if(filebytes>1024*1024*5) {
    alert("The file you selected is TOO LARGE and will be rejected by the server. Please select a smaller file."); 

If you can't work out what is happening there, then "contact_file" is the id from your file input and 1024*1024*5 is the limit you want to impose, in this case 5MB. Don't forget to check that the server side limit is in line with this.

It won't work in IE* or some older browser versions, but it's a nice extra courtesy for users where it can be executed. You could also hook it up to the submit button if you want to do a check at that stage.

*Apparently there's an ActiveX thing you can do for IE users, but I am not so inclined.

Sunday 7 July 2013

How To: International Exchange Rates in iOS Stocks App

I like to keep an eye on the exchange rate for the US Dollar against the British Pound Sterling GBP and the Euro, and have been using the Stocks app built into iOS. I don't have any stocks, so I only use the application for tracking exchange rates and it performs well for my needs.

Over the last few weeks I have shared this functionality with a few friends who also want to keep track of exchange rates and realized that setting these up in the Stocks app is not at all obvious, some would say impossible unless you know the process. Thankfully, once you know the process, it's a piece of cake.

Here are the steps

  1. Open the Stocks app
  2. Tap the ( i ) icon in the bottom right hand corner
  3. Tap the +  icon in the top left corner
  4. Enter the currency rate described as AAABBB=X in the search box (e.g: GBPUSD=X )
  5. Tap the item in the list
  6. Click the "Done" button in the top right corner

How To: Import an Excel CSV file with Sequel Pro

I've been working with a database migration project lately that is bringing together data from three different sources. One of those sources is a big Excel spreadsheet. The data in the spreadsheet has been problematic due as each row included cells with pipe delimited items, typos and other strangenesses that have slowed the pat to a relational database. On top of "ropey data", I also had some problems with importing the data into mySQL.

Usually I would just Save As a CSV (comma separated values) file, and then import the CSV file using OSX application Sequel Pro. This time I was getting fields by split due to some of the fields containing quotes and commas in their text content. I tried to looking for options to use different delimiters, but this is not available Excel (certainly not the version I use), I tried opening the spreadsheet in Google Docs and saving as CSV from there, but this returned the same result. It seemed that Sequel Pro's import engine was not able to interpret the "" encoding of " content and the age-old options of backslashing  \" or custom delimiters are no longer available. Upgrading to the latest version of Sequel Pro didn't help either.

I did a bit of searching and eventually found a source that outlined the solution to the problem. Realistically, it's a simple thing I should've thought of myself, but if I missed it and others have too, I thought it was worth documenting here so that others could save time looking for a solution.

The problem here is that the CSV you have uses Excel-style quote escaping, where escaped double quotes are represented by "" instead of using backslash escaping - \" . 
When you select the CSV file in the import dialog, you can see there's a "Fields escaped by" setting underneath the file selection dialog.  If you change this to a double quote, instead of a backslash, you'll find your file is imported correctly (and your choice will be remembered in future).

Here's an image of what you need to alter for Excel files to be processed properly:

Don't forget that you've made this change when it comes to importing other CSV files. The last part of the quote above states "and your choice will be remembered in future", so you *may* need to change it back to backslash \ for other files.