Using 'HasFile' to check whether a file exists before uploading

Posted on 2006-03-22
Medium Priority
Last Modified: 2008-02-01
I'm writing an aspx form which gathers form data and uploads a file from the user's PC
In its simplest implementation, it seems to upload a file even if there is no file to upload!
In the destination directory, I end up with a properly named file with zero size.

To get around this, I'm trying to use the 'HasFile' property described below
(In practice, I'm using an If... Then... statement to check whether there is a file to upload, upload it and add it to a mail message as an attachment.)

However, since adding 'HasFile' to my code, I keep getting the following error message:
Compiler Error Message: BC30456: 'HasFile' is not a member of 'System.Web.UI.HtmlControls.HtmlInputFile'.

I understand the HasFile property is new in .net Framework Version 2.0 but am 99% certain that I have this version of dotNet - I've just downloaded the latest dotnetfx.exe file and ran a repair of my .net Framework installation in which it states it is version 2.0.


----------Sample Code------------

<% @ Import Namespace="System.Data"       %>
<% @ Import Namespace="System.Data.OleDb" %>
<% @ Import Namespace="System.Web.Mail" %>
<% @ Import Namespace="System.Web.UI.WebControls" %>

Sub Button_Click( s As Object, e As EventArgs )
  Dim strDestinationFilePath As string
  Dim strUploadFileExtension As string
  If (inpFileUp.HasFile) Then
   strUploadFileExtension = Mid(inpFileUp.PostedFile.FileName, InStrRev(inpFileUp.PostedFile.FileName, ".") + 1)
   strDestinationFilePath = strPath & strFilename & strUploadFileExtension    'strPath and strFilename are declared/defined eslewhere
   inpFileUp.PostedFile.SaveAs( strUploadFilePath )
  End If
End Sub

<input id="inpFileUp" type="file" runat="server" />
   runat="server" />

----------------------End of sample code----------------
Question by:Beamson
LVL 15

Expert Comment

ID: 16257171
You need to have Visual Studio 2005 if you are using this.

If you are using another type of editor then I cannot help :)
LVL 39

Accepted Solution

appari earned 400 total points
ID: 16257287

when are you getting the error?
if at runtime,
check the framework version your IIS server is using, asp.net version 2.0 or 1.1(in IIS properties ASP.Net tab check the version)

if you are getting the error at design time then you may be not using VS 2005 as pointed by GavinMannion

Author Comment

ID: 16257458
Thanks Gavin,

I now understand that having .net version2 installed is not enough (version 1.1 was still selected in the properties you pointed out).
Have now selected version 2 and it works.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question