Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 14: Working with Files and Email

Uploading Files

There are many situations in which you’ll want your web application to allow users to upload files to the server. For example, you could create a photo album site to which users could upload images for others to view.

ASP.NET 2.0 offers the FileUpload control for uploading files; it provides a text box and Browse button to allow users to select files from their own computers and transfer them to the server with ease. The FileUpload control can be found in the Standard tab of the Toolbox; Figure 14.10 shows how it looks in Visual Web Developer’s design view.

Figure 14.10. The FileUpload control in Visual Web Developer

The FileUpload control has the following read-only properties:

HasFile

True if the user has uploaded a file, False otherwise

FileName

the name of the file as a string

FileContent

a stream that can be used to read the contents of the file

FileBytes

an array of bytes that can be used to read the contents of the file

PostedFile

an HttpPostedFile object for the uploaded file; this object has properties that can be used to obtain additional data about the file, such as:

ContentLength the file length in bytes

590

Uploading Files

ContentType the MIME type of the file (such as image/gif for a GIF file)1

The FileUpload control also has a method that you’ll find very useful: SaveAs. Although you can get the contents of an uploaded file using the FileContent and FileBytes properties described above, it’s usually more convenient to use the SaveAs method to save files uploaded by users, but not before checking that HasFile is True. The SaveAs method takes as a parameter a string containing the path and the name of the target file.

Let’s test this control out. Create a new web form named FileUpload.aspx in the Learning folder, and populate it with this code:

Visual Basic File: FileUpload.aspx (excerpt)

<%@ Page Language="VB" %>

<%@ Import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub UploadFile(ByVal s As Object, ByVal e As EventArgs) ' Did the user upload any file?

If fileUpload.HasFile Then

' Get the name of the file

Dim fileName As String = fileUpload.FileName

'Upload the file on the server fileUpload.SaveAs(MapPath(fileName))

'Inform the user about the file upload success label.Text = "File " & fileName & " uploaded."

Else

label.Text = "No file uploaded!" End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

<title>File Upload</title>

</head>

<body>

<form id="form1" runat="server">

<asp:FileUpload ID="fileUpload" runat="server" /> <asp:Button ID="uploadButton" runat="server" Text="Upload!"

OnClick="UploadFile" />

1 View the complete list of MIME types at http://www.w3schools.com/media/media_mimeref.asp. Note that there’s no guarantee the MIME type is correct, as it’s easily manipulated by the client.

591

Chapter 14: Working with Files and Email

<br />

<asp:Label ID="label" runat="server"></asp:Label>

</form>

</body>

</html>

If you’re using C#, you should place the following code in the <script runat="server"> section:

C#

File: FileUpload.aspx (excerpt)

<script runat="server">

void UploadFile(Object s, EventArgs e)

{

// Did the user upload any file? if (fileUpload.HasFile)

{

// Get the name of the file

string fileName = fileUpload.FileName;

//Upload the file on the server fileUpload.SaveAs(MapPath(fileName));

//Inform the user about the file upload success label.Text = "File " + fileName + " uploaded.";

}

else

label.Text = "No file uploaded!";

}

</script>

Load the script, and click the Upload! button without selecting a file. The message “No file uploaded!” is displayed, as shown in Figure 14.11.

Figure 14.11. An error arising as a file has not been specified

592