I am uploading documents, and have a basic multiple file upload (from the .NET tutorial) working great. BUT, instead of putting all the files uploaded via the webpage, I want to put them in sub folders. So, I want each file to go in the C:\ directory, in a folder named Uploaded Files, and then, a sub folder with the number of a hiddenfield that is hdn.MatterID... Here's what I tried, but am getting the error, Value of type 'System.IO.DirectoryInfo' cannot be converted to 'String'
code behindProtected
Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.ClickDim uploads As HttpFileCollectionuploads = HttpContext.Current.Request.Files
For i As Integer = 0 To (uploads.Count - 1) If (uploads(i).ContentLength > 0) ThenDim c As String = System.IO.Path.GetFileName(uploads(i).FileName) Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\ + hdnMatterID.Value\") Tryuploads(i).SaveAs(d + c)
Span1.InnerHtml =
"File Uploaded Sucessfully."Catch Exp As ExceptionSpan1.InnerHtml =
"Some Error occured." End Try End IfNext i End Sub
Never make important decisions on a Monday!
![]() |
0 |
![]() |
funluckykitty:
Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\ + hdnMatterID.Value\")
CreateDirectory returns a type DirectoryInfo and you are trying to assign that to a string. That is why you are getting that exception. Are you using the variable "d" somewhere?
Thanks,
Max
Let Me Google That For You!
![]() |
0 |
![]() |
bullpit I thought my code should then be the following to indicate the directory and the file name... (That was were I wanted to use d) Am I close or am I totally off base on how to do this?uploads(i).SaveAs(d + c)
Never make important decisions on a Monday!
![]() |
0 |
![]() |
I think you are trying to get the directory path, right. You can get it like this:
Dim d As DirectoryInfo = Directory.CreateDirectory("C:\UploadedFiles\" & hdnMatterID.Value & "\") Dim fullPath As String = d.FullName Dim filePath As String = fullPath + cIf you get the intellisense after you put a dot after d, you will see some properties. See if any of the properties helps you.
Thanks,
Max
Let Me Google That For You!
![]() |
0 |
![]() |
What I'm trying to do is identify where I want files stored once a user clicks "upload". I want them on the c drive, in the UploadedFiles folder, in the hdnMatterID sub folder (I'm trying to figure out how to add that subfolder with the name of it equal to the value in the hdnMatterID field from the form)
so my path would look like C:\UploadedFiles\123\xxxxx.doc and then the next user who used the form would upload to C:\UploadedFiles\124\xxxx.doc
I can concatenate the hdnMatterID onto the file name, but that isn't what I want. I want to create a subfolder with hdnMatterID and save the files in that subfolder.
Never make important decisions on a Monday!
![]() |
0 |
![]() |
Hmmm...I noticed, in your original code, you have the ID actually enclosed within the double quotes. That is why it is not picking up the value. Try something like this.
Dim d As String = System.IO.Directory.CreateDirectory("C:\UploadedFiles\" & hdnMatterID.Value & "\")
Also, you had noticed, DirectoryInfo has a CreateSubDirectory method. If you cannot create the directory by first method here, we can try CreateSubDirectory.
And as I mentioned earlier, you have to use DirectoryInfor.FullName to get the full path, put it in a string, and then add the file name to this path. I have showed that in my previous post.
Thanks,
Max
Let Me Google That For You!
![]() |
0 |
![]() |
Ohh.. I'm so close... Ok, I've got it creating the subdirectory with the following code, but it won't save the file either in the subdirectory, it just seems to create the subfolder, and then nothing else happens. . any idea why?
If
(uploads(i).ContentLength > 0) Then
Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)Dim d As New System.IO.DirectoryInfo("C:\UploadedUserFiles\")
d.CreateSubdirectory(hdnMatterID.Value)Try
End If
uploads(i).SaveAs("d" + c)
Span1.InnerHtml = "File Uploaded Sucessfully."
Catch Exp As Exception
Span1.InnerHtml = "Some Error occured."
End Try
Never make important decisions on a Monday!
![]() |
0 |
![]() |
This seems to work..
uploads(i).SaveAs(
"C:\UploadedUserFiles\" + (hdnMatterID.Value) + "\" + c)
Never make important decisions on a Monday!
![]() |
0 |
![]() |
Great!
Thanks,
Max
Let Me Google That For You!
![]() |
0 |
![]() |