I'm using an image field in the EditTemplate of FormView to allow my users to upload an image. It works fine in all major browsers, including Safari. But not on the iPad. There I get the error message "object reference not set to an instance of an object"
when the EditTemplate is submitted.
I'm guessing it's because, on the iPad, the FileUpload button in my EditTemplate is "greyed out" (which is a problem in and of itself)...I suppose there's no built-in way for an iPad user (using the iPad version of Safari) to search for an image on the iPad
hard drive to upload (without a special App, I'm guessing)....So, when the EditTemplate form is submitting on an iPad, I get the above error message---I think because the Fileupload field is Null (I have this theory, because the stack trace references my
"ValidateImage" method in my code behind.)
Anybody have any direction or answers I should try? Is there an obvious answer here, or should I post my EditTemplate mark-up and code behind?
You should add a check in your code to see if the Uploaded File is null before trying to manipulate something that might not be there in any case. As far as the IPad is concerned, you can't upload and download files from or to it. File transfer is managed
by dedicated apps.
Sub ValidateImage(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
If Session("Image") = True Then
' There is a file
Dim uploadAvatar As FileUpload = DirectCast(AdvertisementForm.FindControl("uploadAvatar"), FileUpload)
Dim UploadFileName As String = uploadAvatar.PostedFile.FileName
If UploadFileName = "" Then
' There is no file selected
args.IsValid = True
Else
Dim Extension As String = UploadFileName.Substring(UploadFileName.LastIndexOf(".") + 1).ToLower()
' Valid file type
If Extension = "jpg" Or Extension = "bmp" Then
args.IsValid = True
Else
' Not valid file type
args.IsValid = False
End If
End If
Else
Dim uploadAvatar As FileUpload = DirectCast(AdvertisementForm.FindControl("uploadAvatar"), FileUpload)
Dim UploadFileName As String = uploadAvatar.PostedFile.FileName
If UploadFileName = "" Then
' There is no file selected
args.IsValid = False
Else
Dim Extension As String = UploadFileName.Substring(UploadFileName.LastIndexOf(".") + 1).ToLower()
' Valid file type
If Extension = "jpg" Or Extension = "bmp" Then
args.IsValid = True
Else
' Not valid file type
args.IsValid = False
End If
End If
End If
End Sub
Thanks Mike...Above is I think the method that's causing the problem. I believe I've got a check there for when uploadAvatar is empty...Can you find anything amiss here or that I could add to improve it? Thanks.
Dim UploadFileName AsString = uploadAvatar.PostedFile.FileName
You are trying to reference a file here. If there is no file, the code will error because you cannot get a property (FileName) of an object that doesn't exist. You should use the Upload.HasFile property to check to see if anything was uploaded:
Condado
Member
67 Points
181 Posts
How to allow iPad user to use Fileupload?
Feb 13, 2011 12:24 AM|LINK
I'm using an image field in the EditTemplate of FormView to allow my users to upload an image. It works fine in all major browsers, including Safari. But not on the iPad. There I get the error message "object reference not set to an instance of an object" when the EditTemplate is submitted.
I'm guessing it's because, on the iPad, the FileUpload button in my EditTemplate is "greyed out" (which is a problem in and of itself)...I suppose there's no built-in way for an iPad user (using the iPad version of Safari) to search for an image on the iPad hard drive to upload (without a special App, I'm guessing)....So, when the EditTemplate form is submitting on an iPad, I get the above error message---I think because the Fileupload field is Null (I have this theory, because the stack trace references my "ValidateImage" method in my code behind.)
Anybody have any direction or answers I should try? Is there an obvious answer here, or should I post my EditTemplate mark-up and code behind?
Thanks.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: How to allow iPad user to use Fileupload?
Feb 13, 2011 02:06 PM|LINK
You should add a check in your code to see if the Uploaded File is null before trying to manipulate something that might not be there in any case. As far as the IPad is concerned, you can't upload and download files from or to it. File transfer is managed by dedicated apps.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Condado
Member
67 Points
181 Posts
Re: How to allow iPad user to use Fileupload?
Feb 13, 2011 07:46 PM|LINK
Sub ValidateImage(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) If Session("Image") = True Then ' There is a file Dim uploadAvatar As FileUpload = DirectCast(AdvertisementForm.FindControl("uploadAvatar"), FileUpload) Dim UploadFileName As String = uploadAvatar.PostedFile.FileName If UploadFileName = "" Then ' There is no file selected args.IsValid = True Else Dim Extension As String = UploadFileName.Substring(UploadFileName.LastIndexOf(".") + 1).ToLower() ' Valid file type If Extension = "jpg" Or Extension = "bmp" Then args.IsValid = True Else ' Not valid file type args.IsValid = False End If End If Else Dim uploadAvatar As FileUpload = DirectCast(AdvertisementForm.FindControl("uploadAvatar"), FileUpload) Dim UploadFileName As String = uploadAvatar.PostedFile.FileName If UploadFileName = "" Then ' There is no file selected args.IsValid = False Else Dim Extension As String = UploadFileName.Substring(UploadFileName.LastIndexOf(".") + 1).ToLower() ' Valid file type If Extension = "jpg" Or Extension = "bmp" Then args.IsValid = True Else ' Not valid file type args.IsValid = False End If End If End If End SubThanks Mike...Above is I think the method that's causing the problem. I believe I've got a check there for when uploadAvatar is empty...Can you find anything amiss here or that I could add to improve it? Thanks.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: How to allow iPad user to use Fileupload?
Feb 13, 2011 08:26 PM|LINK
You are trying to reference a file here. If there is no file, the code will error because you cannot get a property (FileName) of an object that doesn't exist. You should use the Upload.HasFile property to check to see if anything was uploaded:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile.aspx
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Condado
Member
67 Points
181 Posts
Re: How to allow iPad user to use Fileupload?
Feb 14, 2011 12:16 AM|LINK
Thanks Mike...that worked!
Fedor
Member
62 Points
15 Posts
Re: How to allow iPad user to use Fileupload?
Aug 02, 2011 02:06 AM|LINK
You can upload files using native iOS application. It was discussed here:
http://forums.asp.net/t/1610952.aspx/1?asp+net+upload+file+not+working+for+IPad+IPhone
Fedor Skvortsov
http://www.aurigma.com