Angry Monkeys

Angry Monkeys is a web dev team based in Melbourne. We build software for smarter simians.

Using plupload with ASP.NET

posted February 26th 2010

I hate file uploaders. The simplest way is always the ugliest, and having anything “nice” requires days of backbreaking labour, and even then you’re not even close to being sure it will work across all browsers. It’s frustrating to do so much work and then have some users receive seemingly random errors for no apparent reason!

Matt pointed this neat file uploader out to me the other day, plupload. It is very simple and has about a half dozen “fallbacks” for supporting different technologies from Google Gears, Flash and HTML5.

For the most part implementing plupload is a matter of downloading the zip, unzipping and whacking in some sample code. But there IS a catch. Plupload uses BINAY STREAMING, not your bog standard multipart upload. So if you’re hoping for a simple drag and drop replacement for your existing code, sorry to disappoint. But it’s not that hard to convert your existing app into a binary stream app!

Of course, there is an easy PHP implementation from the folks that wrote plupload, but I really struggled to find an ASP.NET one. So here’s one I wrote based loosely on the PHP version! This is not complete by any stretch (you’d want to handle caching, I/O errors, folder creation etc), but this is the guts of it to save you some research.

Two main things you’ll want to take note of.

1. Plupload supports chunks – this is a way to get around upload file size restrictions. Basically, the file can be chopped up and sent as multiple parts, and then put back together again when it arrives. If you look at the requests going to the server, you will see there are actually multiple requests PER FILE. This means we need to stitch the files together when we’re done at the other end.

2. Data handling – plupload stores the file name, and chunk info into the querystring, and all the file data is in Request.InputStream…so don’t bother trying to work out why Request.Files is empty!

Here is the basics of what you need to implement plupload in asp.net! Easy as! If you’re using any other file uploader, change it NOW!


int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
string fileName = Request.QueryString["name"] != null ? Request.QueryString["name"] : "";

//open a file, if our chunk is 1 or more, we should be appending to an existing file, otherwise create a new file
FileStream fs = new FileStream(Server.MapPath("/files/" + fileName), chunk == 0 ? FileMode.OpenOrCreate : FileMode.Append);

//write our input stream to a buffer
Byte[] buffer = new Byte[Request.InputStream.Length];
Request.InputStream.Read(buffer, 0, buffer.Length);

//write the buffer to a file.
fs.Write(buffer, 0, buffer.Length);
fs.Close();

Of course, if you’re REALLY lazy, they’ve just recently updated plupload to support multipart uploads…but that’s just boring!

Performance Issues and moving host

posted February 19th 2010

Hi all!

It appears we may have outgrown our “budget” hosting solution. Over the last few weeks we’ve seen an anomalous increase in timeouts across the site without a corresponding increase in traffic. I’m pretty sure my programming hasn’t gotten worse, and I’m pretty sure our users haven’t gone feral on us, so the only remaining variable is the shared host we’re on.

I guess it’s a classic case of “you get what you pay for”.

So we’ve decided to move house! I’d be more than happy to hear recommendations from the community for a .NET host supporting SQL2008. If you have any suggestions, drop us an email at support@fivesecondtest.com or contact us on twitter!

The downside of this move is that there may be some interruption to services while we move house. The upside is that when we’re back up, things will be LIGHTNING fast….unless the problems ARE because of my programming…in which case I apologise in advance!

Thanks for your support!

Karma

posted February 09th 2010

The Karma system is here at long last! It’s taken a lot of thinking, a lot of coding, and a lot of tweaking to get this to a point where we’re happy with it. Today is the day when I can finally say we’re ready to release it! When having a look, please keep in mind that this is only phase one of our system. I can’t go into detail as to what else we have planned, but hopefully this is enough to keep everyone happy until “later”.

So what is this karma system?
Well, in a nutshell, we’re now rewarding users who do tests by giving them more responses. This sort of happened indirectly before, but now it is very clear and very defined that when you help the community you get rewarded. In the past if everyone did more tests, everyone got more results. With the changes to the payment structure, our “free” users got pretty well burned. We’re really sorry about that. The karma system was supposed to happen at the same time, and for various reasons didn’t. Well it’s here now, and as an added bonus, we’re applying karma points retrospectively!. That means if you’ve been doing a heap a tests for us over the past few months, you’ll get all that karma in one lump sum! We even have one loyal user who’ll find a stash of nearly 300 karma points!! It’s the least we can do to say thanks for your support!

How does it work?
Put simply, you do one test, you earn one karma point. When you get 10 karma points, you can buy 10 more results for one of your tests. Easy! So this now means free tests get can get up to 15 results without you spending a single dollar. Of course, we’d still like you to spend a dollar, so upgrades can only be done once per test.

What’s the catch?
There is no catch, but there are a couple of conditions. Firstly, you need to be registered. If you’re not registered, you can’t earn karma. Secondly, you can only earn 15 karma at a time. This gets reset every 12 hours. Basically, this is to stop folks just clicking through 60 tests without any real thought as to what they’re doing collecting all the karma, creating some tests for free and then never coming back to the community. Besides, there’s no point in you getting 10 extra results if all the results are garbage! We’re far more interested in rewarding regular visitors who frequently contribute to the community. Of course, by doing more than 15 tests, you’re still helping out the community, so by all means go nuts!

Mega Karma Chain Super Bonus
We have a couple of extra ways to earn bonus karma! If you consistently do 15 or more tests a day, you’ll start to earn a Karma Chain Bonus! This bonus increments each day you keep the chain going up to a maximum of 15. This means you could actually earn 30 karma a day! That’s enough to upgrade 3 tests! The trick is to come back at least 5 times in a week and do 15 tests, that’s when you’ll start seeing the chain bonus.

Community moderation
On top of that, you have the ability to give other users bonus karma directly! In your test results page you now have the ability to reward users by clicking the little green thumbs up. This gives them an extra karma point for that excellent feedback! Of course, if someone submits a garbage response you can always deduct a karma point! Deleting someone’s response will quickly have their karma going backwards, so play nice!

Feedback!
We’d love to hear what you think of our new karma system, so please either leave a comment below, send us an email to or follow us on twitter. There will be many more features surrounding this including a top 10 list, “credit for karma” and some other things I can’t even tell you yet!!