Receiving Files

In contrast to previous versions, ASP.NET makes it relatively easy to receive files uploaded by a client as well as to send files. Request.HttpFilesCollection contains a collection of files sent by the client. To let a client upload a file, place an HTML File Field control on the page. Right-click the control and select Run as Server Control from the context menu. That lets you access the properties of the control in your code-behind class.

There's a small trick involved in getting the File Field control to work properly. Switch to HTML view, find the <form> tag, and add the attribute and value enctype="multipart/form-data" to the tag. The Web Form ch10-9.aspx contains an example. In that Web Form, after adding the attribute, the <form> tag looks like this:

<form id="Form1" method="post" runat="server" enctype="multipart/form-data">

After adding the attribute, you can run the Web Form. The presence of the File Field control in the ASPX file tells the page to create an HtmlFileInput control instance (the difference in the names is confusing). That control is a composite control that contains an input field and a Browse button (see Figure 10.8).

Figure 10.8: Uploading files: the File Field control

Run the ch10-9.aspx Web Form, click the Browse button, and select a text file. Click the Submit button to upload the file. When you click the Upload File button, the server runs this code:

private void cmdUpload Click(object sender, System.EventArgs e) {

StringBuilder sb = new StringBuilder(); // Display information about posted file sb.Append("<b>Information about your uploaded " +

"file</b>:<br><br>"); sb.Append("Name: " + fileInput.PostedFile.FileName + "<br>");

sb.Append("Type: " + fileInput.PostedFile.ContentType +

"<br>"); sb.Append("Size: " +

fileInput.PostedFile.ContentLength.ToString() + "<br><br>");

Figure 10.8: Uploading files: the File Field control

Run the ch10-9.aspx Web Form, click the Browse button, and select a text file. Click the Submit button to upload the file. When you click the Upload File button, the server runs this code:

private void cmdUpload Click(object sender, System.EventArgs e) {

StringBuilder sb = new StringBuilder(); // Display information about posted file sb.Append("<b>Information about your uploaded " +

"file</b>:<br><br>"); sb.Append("Name: " + fileInput.PostedFile.FileName + "<br>");

sb.Append("Type: " + fileInput.PostedFile.ContentType +

"<br>"); sb.Append("Size: " +

fileInput.PostedFile.ContentLength.ToString() + "<br><br>");

divInfo.Visible = true; divInfo.InnerHtml = sb.ToString();

// Save uploaded file to server fileInput.PostedFile.SaveAs(Server.MapPath(".") + "\\uploadfile.txt");

The File Field control's name is fileInput. Most of the code in the method uses a StringBuilder to concatenate information about the file using the fileInput control's properties, which it places into a Label control named divInfo. The method retrieves the uploaded filename, content type (text/plain), and size (see Figure 10.9).

Figure 10.9: Uploading files: information available on the server from the HtmlFileInput control ra-p nMM'.VH

Figure 10.9: Uploading files: information available on the server from the HtmlFileInput control

If you look carefully at the preceding example, you'll notice that the fileInput variable (the File Field control) has a property called PostedFile—which in turn has properties called Filename, ContentType, and ContentLength. This should be a clue that the PostedFile property contains an instance of another class, in this case the HttpPostedFile class. The last line of the code calls the HttpPostedFile object's SaveAs method to save the file to a directory on the server. Interestingly, HttpPostedFile is part of a collection of files uploaded from a client, called the HttpFileCollection, meaning you can upload more than one file at a time from the client. The server parses the data and file meta-information from the Request data and creates the HttpFileCollection when it receives the request.

The File Field HTML control can upload only one file at a time; but you can place more than one File Field control on a page and send both files at the same time with a Submit button.

Was this article helpful?

0 0

Post a comment