This example shows how to upload an image file to a file server and save it in a web friendly format. There are two parts to this - an HTML form which contains an file input box and an Active Server Page to decode the uploaded file and save it as something web friendly.

 

   
1
 
HTML for the form    
     

The HTML for your form should appear something like the following. Note that we are using a multipart encoding, something which is required if you are using the input file-type form element.

[HTML]
<form method="post" action="upload.aspx" name="submit" enctype="multipart/form-data">
  Choose a file to upload:<br>
  <input type="file" name="filefield" size="40"><br>
  <input type="submit" name="submit" value="submit"><br>
</form>

 

   
2
 
Decoding the Data    
     

Your upload.asp page should contain code to decode the uploaded file and save it out in a web friendly format. First we find the physical path to the place we are going to save the file.

[Visual Basic]
Dim path As String
path = Server.MapPath("../images/picture.jpg")

[C#]
string path = Server.MapPath("../images/picture.jpg");

Next we get the uploaded information out of the request. Here we use the standard .NET upload classes but for more control over the upload process you can use ABCUpload.

[Visual Basic]
Dim field As HttpPostedFile
field = Request.Files("filefield")

[C#]
HttpPostedFile field = Request.Files("filefield");

 

   
3
 
Drawing the Data    
     

Finally we create a Canvas, draw the binary data straight onto it and save the result out in the location we identified earlier.

[Visual Basic]
Dim canvas As New Canvas
canvas.DrawStream(field.InputStream, field.FileName, "")
canvas.SaveAs(path, "")

[C#]
Canvas canvas = new Canvas();
canvas.DrawStream(field.InputStream, field.FileName, "");
canvas.SaveAs(path, "");