How to create directories when uploading to a web server?
Posted on 2005-04-25
I want to be able to upload a file to my webserver to a directory named for the current day (i.e. 4-25-2005).
I need it to first check and see if the directory is there. If it is there then just save the file to it.
If it isn't there then it needs to create the directory and then save the file to that created directory.
I am able to write to the directory if I manually created it on my web server. But I don't know how to create the directory if it isn't there.
HOW DO I DO THIS?
Here is my code behind.
Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
Dim myConn As New SqlClient.SqlConnection
Dim myCommand As New SqlClient.SqlCommand
Dim myAdapter As New SqlClient.SqlDataAdapter
Dim CS As String
CS = "Server=DELL;initial catalog=classes;integrated security=true"
myConn.ConnectionString = CS
myCommand.Connection = myConn
myAdapter.SelectCommand = myCommand
If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
Dim dateNow As String = Date.Today
Dim properDate As String = Replace(dateNow, "/", "-")
Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim fileSize As String = File1.PostedFile.ContentLength
Dim SaveLocation As String = Server.MapPath("Data\" + properDate) & "\" & fn
myCommand.CommandText = "INSERT INTO FILES(file_name, file_size, file_path, upload_date) VALUES ('" & fn & "', '" & fileSize & "', '" & SaveLocation & "', '" & DateTime.Now & "')"
Response.Write("The file has been uploaded.")
lblMessage.Text = "Record Successfully Added"
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
Response.Write("Please select a file to upload.")