views:

48

answers:

4

Hi,

I'm developing a website and I'd like the users be able to upload files to the server using a webpage.

The server side is .NET, but it's not ASP.NET. That means, I'm generating the html code myself instead using ASP.NET, and I'm working with the GET string and POST stream directly. It's a good exercise to learn what happens under the hood :D, specially nowadays when there is a framework for everything.

I've been trying to find information about this, but I found several approaches, some of them javascript (thing I want to avoid for the time being) and lots of premade controls. What I want is to do it myself, I don't care if there is a nice, nifty and well-proven ASP.NET control... what I want is understand how it do that with all its implications.

Cheers!

+1  A: 

I think that the following link should contain the information you need:

http://www.cs.tut.fi/~jkorpela/forms/file.html

Basically you need an input type="file" and to set the encoding of your form to multipart/form-data.

Paddy
+1  A: 
<input type="file" name="somename" size="n"> 

You put that in a form, and hasta la vista baby !

Guillaume Lebourgeois
A: 

You can't do a file upload using pure HTML: You will need to handle the uploaded files on server side.

You could parse the uploaded file(s) out of the raw POST data if you want to learn how it works "under the hood" (see here for an example how to do it in ASP), but you will need some kind of server side language to do it.

Pekka
You need a server-side language, but not a server-side framework if you build **all** the server code yourself, which is what the querant seems to be doing (and indeed while a crazy form of reinventing the wheel for a live project, it's a **very** educational exercise).
Jon Hanna
@Jon good point! You could in theory even build a small web server of your own - I think there are several example projects out there. But that goes *deep* into dealing with the HTTP protocol and its quirks. I wouldn't want to do that in a thousand years :) but it definitely is of great educational value.
Pekka
I wrote a simple webserver myself in the 1990s. It wasn't efficient or complete or in anyway able to do the job well enough to be used for a paid-for project, but it was quite fun and it taught me a **lot** of the deep HTTP knowledge that I still use today.
Jon Hanna
+2  A: 

In the HTML you need a form with an input of type="file" and the enctype attribute of the form set to "multipart/form-data" rather than the default of "application/x-www-form-urlencoded".

Multipart/form-data is defined in RFC 2388, and will behave differently to the application/x-www-form-urlencoded you've been parsing with this experiment so far, though it's quite straight-forward. The RFC should give you all you need to know to replicate how the HttpRequest.Files property works in ASP.NET.

An extension of this, try sending streams from XMLHttpRequest in a page or HttpWebRequest in a .NET client application, using both POST and PUT (you may have to change IIS settings to allow the PUT through), as this the overlap of working on that along with your experiments here will cover some knowledge that has some real applicability even when you are using all the toolkits. Another extension is to try implementing both sides of both schemes in RFC2617 without any help from the framework (sometimes the server side of this is genuinely useful).

Kudos for experimenting with this, it should bring real experience to back up what you can learn from reading RFC 2616 (though that's still absolutely vital for anyone doing web stuff to be intimiately familiar with, as reading will cover some cases your experiments don't touch on, and explain anything that seems strange in your results).

Jon Hanna
+1 the best answer so far!
Pekka
Thanks very much, I'm gonna try and see what I can get :)Kind regards.
vtortola