I'm using request.files as HttpFileCollectionBase and passing them into the Class File using a public variable. The count property is 1 even no file is upload so I don't know how to if file upload before call the construct method.
I also cannot iterate the collection. The P_ variables are class private passed in from the public property assignments.
The construct method:
Code:
Private p_filesToAttach As HttpFileCollectionBase
Public Property FilesToAttach() As HttpFileCollectionBase
Get
Return p_filesToAttach
End Get
Set(ByVal value As HttpFileCollectionBase)
p_filesToAttach = value
End Set
End Property
Public Sub ConstructUpload()
' Get full path to the attachments folder
Dim FolderPath As String = Current.Server.MapPath(p_attachmentFolder) + "\"
Dim strFileName As String = ""
Dim httpFiles = p_filesToAttach
Dim i = p_filesToAttach.Count
Try
For Each upfile In httpFiles
'Dim upfile As HttpPostedFileBase =
If upfile.ContentLength > 0 Then
Current.Response.Write("this is it")
Current.Response.End()
End If
strFileName = Path.GetFileName(upfile.FileName)
'verify file exists
If Not String.IsNullOrEmpty(strFileName) Then
'set file paths
Dim filePath As String = FolderPath + GetFilePath(strFileName)
If Not (upfile) Is Nothing Then
Current.Response.Write(p_filesToAttach.Count)
Current.Response.End()
End If
'check file size
If upfile.ContentLength > (250 * 1000) Then
Throw New WebException("Upload file:" & strFileName & " exceeds the allowed size of: " & (p_attachmentMaxLength * 1000) & " kilobytes.")
Else
'Create the directory if it does not exist.
If (Not Directory.Exists(FolderPath)) Then
If CheckFolderPermissions(FolderPath) Then
Directory.CreateDirectory(FolderPath)
Else
Throw New WebException("You don't have permission to attach the file: " & strFileName & ".")
End If
End If
' Delete existing files with same name before uploading new.
If File.Exists(filePath) Then
If CheckFolderPermissions(FolderPath) Then
File.Delete(filePath)
Else
Throw New WebException("You don't have permission to attach the file: " & strFileName & ".")
End If
End If
' Call UploadAttachments to upload the file.
UploadFiles(upfile, upfile.FileName, FolderPath)
End If
Else
Throw New WebException("Upload file cannot be found.")
End If
Next
Catch e As WebException
ErrorCodes.Add(e)
End Try
End Sub ' ConstructUpload