Craig Beamson
asked on
Using 'HasFile' to check whether a file exists before uploading
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
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile(VS.80).aspx
(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.HtmlControl s.HtmlInpu tFile'.
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.
ANY IDEAS HOW TO FIX MY CODE SO THIS WORKS AS INTENDED?
----------Sample Code------------
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.Ole Db" %>
<% @ Import Namespace="System.Web.Mail " %>
<% @ Import Namespace="System.Web.UI.W ebControls " %>
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.F ileName, InStrRev(inpFileUp.PostedF ile.FileNa me, ".") + 1)
strDestinationFilePath = strPath & strFilename & strUploadFileExtension 'strPath and strFilename are declared/defined eslewhere
inpFileUp.PostedFile.SaveA s( strUploadFilePath )
End If
End Sub
...
...
<input id="inpFileUp" type="file" runat="server" />
<asp:Button
Text="submit"
OnClick="Button_Click"
runat="server" />
----------------------End of sample code----------------
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
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile(VS.80).aspx
(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.HtmlControl
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.
ANY IDEAS HOW TO FIX MY CODE SO THIS WORKS AS INTENDED?
----------Sample Code------------
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.Ole
<% @ Import Namespace="System.Web.Mail
<% @ Import Namespace="System.Web.UI.W
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.F
strDestinationFilePath = strPath & strFilename & strUploadFileExtension 'strPath and strFilename are declared/defined eslewhere
inpFileUp.PostedFile.SaveA
End If
End Sub
...
...
<input id="inpFileUp" type="file" runat="server" />
<asp:Button
Text="submit"
OnClick="Button_Click"
runat="server" />
----------------------End of sample code----------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
If you are using another type of editor then I cannot help :)