I am using an AsyncFileUpload control inside a FormView. I only want the file to saved during the FormView's ItemInserting event. In trying to find a way to do this, the only thing I could come up with was to store the AsyncFileUpload control or the PostedFile in a Session variable during the OnUploadedComplete() event and then invoke the SaveAs method in the ItemInserting event. Problem is the SaveAs method is throwing an exception saying "Cannot Access A Closed File". If I invoke SaveAs in the OnUploadedComplete() event, it saves successfully, but I need it to only save when the user submits the form. Any ideas on this error or perhaps a better way to save the file during ItemInserting()?
Protected Sub frmAddDocument_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles frmAddDocument.ItemInserting
Try
Dim txtFile As AsyncFileUpload = Session("Doc")
txtFile.SaveAs(Server.MapPath("Documents") & "\" & txtFile.FileName)
Catch ex As Exception
lblAddErrorMessage.InnerHtml = ex.Message
e.Cancel() = True
End Try
End Sub
Protected Sub txtFile_OnUploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)
Session("Doc") = sender
End Sub