Could not find a part of the path "\ASP.Net Practical\Website\Site1\UploadedImgs \"..
-
Good day friends! Am having problem with my asp.net project. I have two buttons on the web page. One save to “Temp Files” and the other save to “UploadedImgs”. Saving to the “Temp Files” folder do not raise any exception but saving to the “UploadedImgs” folder causes an exception which says “Could not find a part of the path 'D:\ASP.Net Practical\Website\Site1\ UploadedImgs \'.” To me, the path is 100% correct. So please, does anyone has solution to this problem? Any help will be appreciated. Below is the code am using.
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click Dim imgext As String = Path.GetExtension(FileUpload1.PostedFile.FileName) 'get image extension Dim Imgname As String = Path.GetFileName(FileUpload1.PostedFile.FileName) 'get image name Dim imgpath As String = "\ASP.Net Practical\Website\Site1\ UploadedImgs \" 'get local disk folder path FileUpload1.PostedFile.SaveAs(Server.MapPath(imgpath + Imgname + imgext)) End Sub
-
Good day friends! Am having problem with my asp.net project. I have two buttons on the web page. One save to “Temp Files” and the other save to “UploadedImgs”. Saving to the “Temp Files” folder do not raise any exception but saving to the “UploadedImgs” folder causes an exception which says “Could not find a part of the path 'D:\ASP.Net Practical\Website\Site1\ UploadedImgs \'.” To me, the path is 100% correct. So please, does anyone has solution to this problem? Any help will be appreciated. Below is the code am using.
Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click Dim imgext As String = Path.GetExtension(FileUpload1.PostedFile.FileName) 'get image extension Dim Imgname As String = Path.GetFileName(FileUpload1.PostedFile.FileName) 'get image name Dim imgpath As String = "\ASP.Net Practical\Website\Site1\ UploadedImgs \" 'get local disk folder path FileUpload1.PostedFile.SaveAs(Server.MapPath(imgpath + Imgname + imgext)) End Sub
Clean the save filename first, remove or strip invalid chars, then build the path based on a virtual path, not a physical path. The path will then be converted to a physical path with the Drive letter and all.
Dim fileName as String = Clean_FileName(avatarName.ToLower() + oFileExt)
Dim path as string = Path.Combine(Server.MapPath("~/Images/avatars/customers"), fileName)Public Shared Function Clean_FileName(ByVal strIn As String) As String
' Replace invalid characters with empty strings.
Try
Return Regex.Replace(strIn, "[^\w\.@-_]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5))
Catch As RegexMatchTimeoutException
Return String.Empty
End TryEnd Function
-
Clean the save filename first, remove or strip invalid chars, then build the path based on a virtual path, not a physical path. The path will then be converted to a physical path with the Drive letter and all.
Dim fileName as String = Clean_FileName(avatarName.ToLower() + oFileExt)
Dim path as string = Path.Combine(Server.MapPath("~/Images/avatars/customers"), fileName)Public Shared Function Clean_FileName(ByVal strIn As String) As String
' Replace invalid characters with empty strings.
Try
Return Regex.Replace(strIn, "[^\w\.@-_]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5))
Catch As RegexMatchTimeoutException
Return String.Empty
End TryEnd Function
Solved: Thanks. I found the solution after so many trials. I think I should share this so that anyone with such problem could resolve it with ease. The problems was from my webpage submitting an empty filename because of the page load event. So, the solution to this problem is ensure that the uploaded filename is intact before calling the SaveAs command. Once again, thanks for your reply. Cheers!
-
Solved: Thanks. I found the solution after so many trials. I think I should share this so that anyone with such problem could resolve it with ease. The problems was from my webpage submitting an empty filename because of the page load event. So, the solution to this problem is ensure that the uploaded filename is intact before calling the SaveAs command. Once again, thanks for your reply. Cheers!
Well yes that's a good idea. But the way your mapping out the path may backfire on you when you publish your web app for production use. Uploading files of anything requires good knowledge of how it works. There about 5 ways to do it to my knowledge. I don't use the file name of the file anymore, for now I assign my own name to the file. Later on down the road, as users keep uploading the same file with different names, you will have gigs of files sitting in the folder, and the project size will radically expand in size over the course of time. Then what? That's another topic for discussion.