Get Uploaded File Info

What information can you get about an uploaded file and how do you do it?

asp.netNOW Q&A

LANGUAGES: VB .NET

ASP.NET VERSIONS: 1.0 | 1.1

 

Get Uploaded File Info

What information can you get about an uploaded file and how do you do it?

 

By Josef Finsel

 

Q. Is there a way in ASP.NET to validate the file size, image type, and image attributes when a user uploads a picture file (BMP, JPG, GIF, etc.)?

- JN, Brighton, OK

 

A. There sure is. It just takes a little work and a couple of different pieces of code. Let's start with the HTML elements that you'll use for uploading a file. Figure 1 shows the HTML. Any HTML 3.2-compliant browsers should support the INPUT file tag to allow uploading of files, and by setting the encType property of the form you are setting the browser's encoding to allow binary data.

 

<form id="LoadImage" action="loadimages.aspx" method="post"

           encType="multipart/form-data" runat="server">

<P>Select File To Upload to Server:

<input id="ImageFile" type="file" runat="server"

           NAME="ImageFile">

<asp:Button id="btnUpload" runat="server"

           Text="Upload File"></asp:Button>

</form>

<div id="FileDetails" runat="server">

FileName:<span id="FileName" runat="server"><br>

ContentType:

<span id="MyContentType" runat="server">

<br>

ContentLength:

<span id="ContentLength" runat="server">

bytes<br>

Img Ref:

<span id="ImageRef" runat="server">

</div>

Figure 1. This HTML code lets you upload a file to the server. The most important things are the INPUT type=file element and the encType="multipart/form-data" in the form.

 

The more impressive code lies on the server side with how you deal with the file once the server gets it. First, you check ImageFile.PostedFile.FileName to see if it contains data. If it does, a file has been uploaded and you can process it. The PostedFile property contains most of the data you want to access, such as ContentLength (file size), ContentType (Image, Text, etc.), and FileName.

 

It's important to note that the file name is the name of the file on the client side, and includes the full path. That means that you need to take the file name and remap it to the server, then save the file. Yes, I did say you need to save the file. It's been posted to the form but that doesn't mean it exists anywhere except within the form itself. This is actually a simple procedure, handled in Figure 2. Remember that the account the Web service is running under will need to access the directory where you plan to store the accounts.

 

strFileName = ImageFile.PostedFile.FileName

If InStr(strFileName, "\") > 0 Then

   strFileName =

    strFileName.Substring(strFileName.LastIndexOf("\") + 1)

End If

'Get web page path  to know where to store the file

strPath = Page.Request.MapPath(Page.Request.FilePath)

If InStr(strPath, "\") > 0 Then

   strPath = strPath.Substring(0, strPath.LastIndexOf("\") + 1)

End If

strNewFileName = strPath & strFileName

ImageFile.PostedFile.SaveAs(strNewFileName)

Figure 2. Here's the code to save the uploaded file to the server. You strip the path information and replace it with the server's Web path (or other path where you want to store the file).

 

For image files, you can now get more information on the newly uploaded file using System.Drawing.Bitmap. Just load the saved file into the image, then you can get the resolution, height, width, and other image-related information. This is as simple as the following code (just check to see if the content type is an image and then process it if it is):

 

Dim myPic As System.Drawing.Bitmap

If strContentType.Substring(0, 5) = "image" Then

  myPic = New System.Drawing.Bitmap(strNewFileName)

  Response.Write("Height")

  MiscImageInfo.InnerHtml = "Height: "

  MiscImageInfo.InnerHtml += myPic.Height.ToString() & "<br>"

  MiscImageInfo.InnerHtml += "Width: "

  MiscImageInfo.InnerHtml += myPic.Width.ToString() & "<br>"

  MiscImageInfo.InnerHtml += "Vertical Resolution: "

  MiscImageInfo.InnerHtml += myPic.VerticalResolution.ToString()

  MiscImageInfo.InnerHtml += "<BR>Horizontal Resolution: "

  MiscImageInfo.InnerHtml += myPic.HorizontalResolution.ToString()

End If

 

Well, that's it for this column. Keep your ASP.NET questions coming to me at: [email protected].

 

The sample code in this article is available for download.

 

Josef Finsel is a software developer specializing in .NET and SQL Server with Avanade, the premier global technology integrator for Microsoft solutions in the enterprise. He has published a number of VB, .NET, and SQL Server articles and, when he isn't hanging around the aspnetpro forums, you can find him working on his own take on Shakespeare's classics. He's also author of The Handbook for Reluctant Database Administrators (Apress).

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish