We had to setup and run a webcam for the Q Awards red carpet earlier this week.
The original plan was to have an old Axis ethernet webcam, connected to a wifi network using a Dlink bridge. After a nightmare of not being able to get the Dlink to work, we gave up and went for a USB webcam connected to a laptop approach.
I had to write some software to handle the capture of the image and the upload to the webserver. Because we wanted to do a few custom things on the way we weren’t able to use out of the box software.
I’ll post on how to use a webcam with .NET another time. What I wanted to document was how to upload a file using .NET to a LAMP server.
It turns out to be easier than I thought for .NET, one line of code can achieve this using the My.Computer.Network.UploadFile Method.
For example, to upload test.txt
to http://www.robertprice.co.uk/asp_upload.pl
we can do the following (in Visual Basic)…
My.Computer.Network.UploadFile("C:test.txt","http://www.robertprice.co.uk/asp_upload.pl")
Now we need some code at the other end to store this upload. As I was building a webcam application, the uploaded file, an image in this case, was always overwritten.
The uploaded .NET file uses an bog standard HTTP upload as described in RFC 1867.
If we use Perl to read this file, we can use the standard CGI.pm module to do most of the hard work for us.
The uploaded file is passed a parameter called file, so we can create a CGI object and get a reference to this using the param
method. Once we have this, we can treat it as a filehandle and read the date off it. Then all we have to do is to write it out to a file.
The following code achieves this.
#!/usr/bin/perl -w
use strict;
use CGI.pm;
my $CGI = new CGI;
my $file = $CGI->param('file');
open(FILE, ">/path/to/my/file/test.txt") or die $!;
while(<$file>) {
print FILE $_;
}
close(FILE);
print "Content-type: text/htmlnn";
print "OKn";