You can certainly use code to get/determine the logon, or use session() code in the 3 AjaxToolKit file uploader events.
The only issue to keep in mind, since the upload is now ajax, then controls and values on the page of course cannot be used, since the web page is not being posted back to the server when those upload events are completed.
So, nothing should prevent use of say session() in those events.
I would suggest that you on the page load event (with the AJ fileuploader), you setup some session value in the page load event that sets the users folder in question.
Hence, say some code like this on page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Session("UserName") = "Albert" ' testing
Session("UpLoadFolder") = Server.MapPath("~/UpLoadFiles/Albert") ' testing
LoadGrid()
myfiles.Visible = False
myupload.Visible = True
End If
End Sub
Sub LoadGrid()
GVFiles.DataSource = MyRst("SELECT * FROM MyFiles ORDER BY UpLoadDate")
GVFiles.DataBind()
End Sub
Protected Sub AjaxFileUpload1_UploadComplete(sender As Object, e As AjaxControlToolkit.AjaxFileUploadEventArgs)
' save file to UpLoadFiles folder
Dim strF As String = Session("UpLoadFolder") & e.FileName
AjaxFileUpload1.SaveAs(strF)
' add this row to database.
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL As New SqlCommand("INSERT INTO MyFiles
(UserName, FileName, SaveFilePath, UpLoadDate)
VALUES (@User, @FileName, @FPath, @UpLoadDate)", conn)
cmdSQL.Parameters.Add("@User", SqlDbType.NVarChar).Value = Session("UserName")
cmdSQL.Parameters.Add("@FileName", SqlDbType.NVarChar).Value = e.FileName
cmdSQL.Parameters.Add("@FPath", SqlDbType.NVarChar).Value = strF
cmdSQL.Parameters.Add("@UpLoadDate", SqlDbType.DateTime).Value = DateTime.Now
conn.Open()
cmdSQL.ExecuteNonQuery()
End Using
End Using
End Sub
So, as above shows, use of session() is permitted in such upload events.
The above thus results in this:
So, keep in mind that the ajaxfile upload events:
They can NOT see nor use textboxes or controls on the page - the page does not have a post back, and thus you should consider the 3 server events as like a static method.
The 3 events can use session, but the events can NOT use ViewState, since once again, that is client side browser side information - not available in the 3 events for the AjaxFileUpLoader.
After all events are completed, the page is not posted back, and OFTEN I do want extra information such as user comments and other information entered by the user on that given page.
Hence, I often add a final button on the page, hide the button, and have the "final" upload ALL event trigger this button, and thus the result is a final whole page post-back that occures AFTER all the files been upload. For example, in above after all files are done uploading, note how I switched my button/tab to show the uploaded files.
This final code is triggered using JavaScript (jQuery in this example).
hence this:
<h4>Select Files to upload</h4>
<br />
<ajaxToolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server"
OnUploadComplete="AjaxFileUpload1_UploadComplete"
OnClientUploadCompleteAll="alldone" />
</div>
<asp:Button ID="cmdAllDone" runat="server" Text="Hidden all done upload Button"
OnClick="cmdAllDone_Click" ClientIDMode="Static" Style="display: none" />
<script>
function alldone() {
$('#cmdAllDone').click()
}
</script>
So note how I added a client side JavaScript event to the AjaxFileUpLoad, and it runs the client side JavaScript routine called "alldone".
And "all done" simply clicks the button I placed on the page. this results in a full page post back, and thus my code behind is free to do some final additional work, or in this case, toggle the div that shows all files uploaded.
Hence the cmdAllDone_Click runs after all files are upload. This code:
Protected Sub cmdAllDone_Click(sender As Object, e As EventArgs)
myfiles.Visible = True
myupload.Visible = False
LoadGrid() ' re-load re-fresh the GridView of files
MyTabs.SelectedIndex = 1
End Sub
So, that final button click is really just a FYI as to how you can have some server side code run, and have a "often" needed post-back after all files been uploaded.
However, the AJ FileUpLoad most certainly does allow use of session(). However, use of Viewstate(), or use of controls from the page are not available and can't be used.