1. Home
  2. Computing & Technology
  3. Delphi Programming

How to change the web form's enctype attribute (to "multipart/form-data")

By , About.com Guide

When using HTML File Upload control to add file upload processing (from client to server) in an asp.net web form, it is very important that the (server-side) form tag includes the enctype="Multipart/Form-Data" attribute.

If you are adding an HTMLInputFile dynamically on a web page (that is if you cannot be sure whether a particular web form will host an <input type="file" runat="server" /> server element), you might be tempted to change the form encoding from code.

The best way to ensure the form's enctype is set to "Multipart/Form-Data" is to attach a simple javascript call to the client side onClick event handler of a button (or any other web control that will handle file upload) that will make sure form's encoding is correct.

Here's a simple line of code that needs to be assigned in the Page_Load event handling procedure for a web form:

UploadButton.Attributes.Add('onclick','javascript:document.forms[0].encoding = "multipart/form-data";')

When the UploadButton is clicked, the client side onclick event is executed; a simple javascript call is used to ensure the form tag has the correct encoding. The process can now take place on the server (Click event of the Button gets fired and executed) ...

Delphi tips navigator:
» How to extract the URL from an Internet Shortcut (.url) file
« Delphi for .Net Code Folding keyboard shortcuts

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. How to change the enctype attribute (to "multipart/form-data") in ASP.Net

©2009 About.com, a part of The New York Times Company.

All rights reserved.