Some occasional musings on mountain bike rides and walks in North Yorkshire, with odd bit of discussion on mapping technology thrown in for good measure
Been going nuts over this one. I have just been adding the ability to add custom waypoint icons to ShareMyRoutes.com, and couldn't, no matter how I tried, get a stream created by the fileupload control, to copy to a byte array, in order to be inserted into the database. Dead easy. Right? I got some old code, and changed it around and it didn't work. I tried code samples, which just didn't work. no errors. But nothing in the byte array either. The stream had data in it, as I was able to do other stuff with the stream.
Well, it looks like it's changed in CLR 2.0. Now, there a few extra properties on the fileupload control, which deal with the stream. If the stream contains a binary file (like an image), you'll need to use something like this:
bytIconImage = FileUploadImage.FileBytes
You can then do what you will with the byte array. Don't forget to check hasfile first, to make sure there's a file. In fact as you are allowing anybody to upload a chunk of potentially unknown binary data, it's good practise to check the extension and the content type, to make sure it's what you expect. Otherwise, you could be opening yourself up to trouble, as uploading nasty stuff is an easy attack vector. All very nice, and less lines of code than the previous way, which involved grabbing a stream from the uploaded file and using stream.read(byteArray, 0, contentLength). Shame the old way of doing it break silently.
Anyway. It's all here, if you need an example:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filebytes(VS.80).aspx
Sorry, comments on this posting are now closed.
Latest postings