Using the HttpPostedFile

As stated in the previous section, the HttpPostedFile class enables programmatic access to the uploaded file on the server. This class exposes a number of valuable attributes that you can use to work with the uploaded file and is accessed through the HtmlInputFile's PostedFile property. These attributes include:

  • ContentLengthThis property Gets the size of the uploaded object in bytes. You can use this property to verify that an object was indeed uploaded, and that it doesn't exceed any size restrictions you might have for uploaded files.

  • ContentType— This property Gets the MIME content type of the object uploaded. You can use this attribute to properly display an image after it is inserted into the database.

  • FileName— This property Gets the full page of the uploaded file—this path is the path from the client's machine. You can use this attribute to retrieve the file name and extension of the uploaded object

  • InputStream— This property Gets a Stream object, which points to the uploaded file. You can use this to put the binary data from the image file into a byte array so that you can insert it into a database.

Listing 13.2 illustrates working with the file after it is uploaded. I will be illustrating how to use all the above attributes except for the InputStream attribute, which will be covered, in the section following this one.

Listing 13.2. Working with the Uploaded File
[VisualBasic.NET]

01: <%@ Import Namespace="System.IO" %>
02: <script language="vb" runat="server">
03:
04:  public sub SubmitImage(sender as Object, e as EventArgs)
05:   dim UpFile as HttpPostedFile = UP_FILE.PostedFile
06:   dim sMessage as new StringBuilder()
07:
08:   if UpFile.ContentLength = nothing then
09:
10:    sMessage.Append("You must pick a file to upload")
11:
12:   else
13:    '//File Path
14:    dim FilePath as string = UpFile.FileName
15:     sMessage.Append("<b>File Path:</b>&nbsp;")
16:     sMessage.Append(FilePath)
17:     sMessage.Append("<br>")
18:    '//File Name
19:    dim FileName as string = Path.GetFileName(FilePath)
20:     sMessage.Append("<b>File Name:</b>&nbsp;")
21:     sMessage.Append(FileName)
22:     sMessage.Append("<br>")
23:    '//File Extension
24:    dim FileExtension as string = Path.GetExtension(FileName)
25;     sMessage.Append("<b>File Extension:</b>&nbsp;")
26:     sMessage.Append(FileExtension)
27:     sMessage.Append("<br>")
28:    '//Content Type
29:    dim ContentInfo as string = UpFile.ContentType
30:     sMessage.Append("<b>Content Type:</b>&nbsp;")
31:     sMessage.Append(ContentInfo)
32:     sMessage.Append("<br>")
33:    '//File Size
34:    dim FileSize as string = UpFile.ContentLength.ToString() & " Bytes"
35:     sMessage.Append("<b>File Size (in Bytes):</b>&nbsp;")
36:     sMessage.Append(FileSize)
37:     sMessage.Append("<br>")
38:    '//Save File Under New Name
39:    dim SaveAsFilePath as string
40:    '//If it is a gif save to images/jpg directory
41:    if (FileExtension = ".jpg") then
42:
43:     SaveAsFilePath = Server.MapPath("images/jpg") + "WhateverWhenever" + FileExtension
44:     UpFile.SaveAs(SaveAsFilePath)
45:
46:    '//If it is a jpg save to images/gif directory
47:    else if (FileExtension = ".gif") then
48:
49:     SaveAsFilePath = Server.MapPath("images/gif") + "WhateverWhenever" + FileExtension
50:     UpFile.SaveAs(SaveAsFilePath)
51:
52:    else
53:
54:     SaveAsFilePath = Server.MapPath("images") + "WhateverWhenever" + FileExtension
55:     UpFile.SaveAs(SaveAsFilePath)
56:
57:    end if
58:
59:    '//New File path
60:     sMessage.Append("<b>SaveAs File Path:</b>&nbsp;")
61:     sMessage.Append(SaveAsFilePath)
62:     sMessage.Append("<br>")
63:    '//Show saved image in image control
64:     sMessage.Append("<b>Uploaded Image:</b>&nbsp;")
65:     sMessage.Append("<br>")
66:     sMessage.Append("<img src='")
67:     sMessage.Append(SaveAsFilePath)
68:     sMessage.Append("' border='0'>")
69:
70:   end if
71:
72:   message.Text = sMessage.ToString()
73:
74:  end sub
75:
76: </script>

[C#.NET]

01: <%@ Import Namespace="System.IO" %>
02: <script language="c#" runat="server">
03:
04:  void SubmitImage(Object sender, EventArgs e) {
05:   HttpPostedFile UpFile = UP_FILE.PostedFile;
06:   StringBuilder sMessage = new StringBuilder();
07:
08:   if (UpFile.ContentLength == 0) {
09:
10:    sMessage.Append("You must pick a file to upload");
11:
12:   }  else  {
13:    //File Path
14:    string FilePath = UpFile.FileName;
15:     sMessage.Append("<b>File Path:</b>&nbsp;");
16:     sMessage.Append(FilePath);
17:     sMessage.Append("<br>");
18:    //File Name
19:    string FileName = Path.GetFileName(FilePath);
20:     sMessage.Append("<b>File Name:</b>&nbsp;");
21:     sMessage.Append(FileName);
22:     sMessage.Append("<br>");
23:    //File Extension
24:    string FileExtension = Path.GetExtension(FileName);
25:     sMessage.Append("<b>File Extension:</b>&nbsp;");
26:     sMessage.Append(FileExtension);
27:     sMessage.Append("<br>");
28:    //Content Type
29:    string ContentInfo = UpFile.ContentType;
30:     sMessage.Append("<b>Content Type:</b>&nbsp;");
31:     sMessage.Append(ContentInfo);
32:     sMessage.Append("<br>");
33:    //File Size
34:    string FileSize = UpFile.ContentLength.ToString() + " Bytes";
35:     sMessage.Append("<b>File Size (in Bytes):</b>&nbsp;");
36:     sMessage.Append(FileSize);
37:     sMessage.Append("<br>");
38:    //Save File Under New Name
39:    string SaveAsFilePath;
40:    //If it is a gif save to images/jpg directory
41:    if (FileExtension == ".jpg") {
42:
43:     SaveAsFilePath = Server.MapPath("images/jpg") + "\WhateverWhenever" + FileExtension;
44:     UpFile.SaveAs(SaveAsFilePath);
45:
46:    //If it is a jpg save to images/gif directory
47:    }  else if (FileExtension == ".gif") {
48:
49:     SaveAsFilePath = Server.MapPath("images/gif") + " WhateverWhenever" + FileExtension;
50:     UpFile.SaveAs(SaveAsFilePath);
51:
52:    }  else {
53:
54:     SaveAsFilePath = Server.MapPath("images") + "\WhateverWhenever" + FileExtension;
55:     UpFile.SaveAs(SaveAsFilePath);
56:
57:    }
58:
59:    //New File path
60:     sMessage.Append("<b>SaveAs File Path:</b>&nbsp;");
61:     sMessage.Append(SaveAsFilePath);
62:     sMessage.Append("<br>");
63:    //Show saved image in image control
64:     sMessage.Append("<b>Uploaded Image:</b>&nbsp;");
65:     sMessage.Append("<br>");
66:     sMessage.Append("<img src='");
67:     sMessage.Append(SaveAsFilePath);
68:     sMessage.Append("' border='0'>");
69:
70:   }
71:
72:   message.Text = sMessage.ToString();
73:
74:  }
75:
76: </script>

[VisualBasic.NET & C#.NET]

78: <html>
79: <body>
80: <form enctype="multipart/form-data" runat="server">
81:
82: <h1>Working With File Names</h1>
83:
84: <asp:Table runat="server" width="800" align="left" >
85:  <asp:TableRow>
86:   <asp:TableCell>
87:    <b>Upload New Image</b>
88:   </asp:TableCell>
89:   <asp:TableCell>
90:    <input type="file" id="UP_FILE" runat="server"
91:     Size="34" accept="image/*" />
92:   </asp:TableCell>
93:  </asp:TableRow>
94:  <asp:TableRow>
95:   <asp:TableCell>
96:     &nbsp;
97:   </asp:TableCell>
98:   <asp:TableCell>
99:    <asp:Button runat="server" width="239"
100:     OnClick="SubmitImage" text="Upload Image"/>
101:   </asp:TableCell>
102:  </asp:TableRow>
103:  <asp:TableRow>
104:   <asp:TableCell>
105:     &nbsp;
106:   </asp:TableCell>
107:   <asp:TableCell>
108:    <asp:Label runat="server" id="message"
109:     forecolor="red" maintainstate="false" />
110:   </asp:TableCell>
111:  </asp:TableRow>
112: </asp:Table>
113:
114: </form>
115: </body>
116: </html>

For Listing 13.2, I created two new sub-directories in the Images directory to demonstrate how to selectively save different image types to different directories. The two new directories are named jpg and gif.

In this example we demonstrate many different ways to extract and manipulate data of the file that was uploaded. First, on line 14, I retrieve the uploaded files path by using the HttpPostedFile classes FileName property—the FileName property retrieves the full path of the file from the clients machine (C:MyImage.Gif). On line 19, I use members of the Path class from the System.IO namespace to extract the actual file name from the path. Specifically, I use the GetFileName method and pass in the full file path I retrieved on line 14. On line 24, I retrieve the extension of the file uploaded by using the GetExtension method of the Path class passing in FileName retrieved on line 19. On line 29, I retrieve the ContentType of the uploaded file by using the HttpPostedFile classes ContentType property (ex: image/gif). This is important information when displaying the image after it is saved in the database, as you will see in a later section. Next, on line 34, I retrieve the size of the file in bytes by using the ContentLength property of the HttpPostedFile class. In lines 39–57, I create a new file name for the image and save it to hard disk. I use an if statement to determine in what directory to save the file by checking the FileExtension variable. If the value of the FileExtension is .jpg then it is saved to images/jpg. If the value is .gif then it is saved to images/gif. All other image types are saved to images. In lines 64–68 I show the newly uploaded image by creating an <img> element and setting its SRC attribute equal to the path of the image using the SaveAsFile variable. The last thing I do is set the Message Label controls Text attribute to show all the data. Figure 13.3 shows the page after an image has been uploaded.

Figure 13.3. All file information and image is displayed on the page.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.120.200