Last post Jul 22, 2015 10:23 PM by Li Wang
Jul 21, 2015 10:47 PM|houman|LINK
I know this question has been asked previously and been answered by a number of people but frankly none of them work for me and none of them make sense to me in my problem. I have a very simple asp.net page that is allowing the user to upload a file (say
an image) through a FileUpload control. My code then simply saves the file on the disk. Here's the twist. If I simply save the file to Sever.MapPath("~/") location, the page saves the uploaded file with no issue. However, I would like to organize the uploaded
files and put them in subfolders. So I have added a utility class called FileHandler which gets a fileNumber and creates some subdirectories under Server.MapPath("~/") location. I check and all the folders and subfolders have been created with no issue. Now
when I try to save the uploaded file to the newly created subfolder, I get the access is denied exception. Here's the relevant part of my code:
if (FileUpload.PostedFile.ContentType == "image/jpeg")
if (FileUpload.PostedFile.ContentLength < 1024000000)
string filename = Path.GetFileName(FileUpload.FileName);
fileHander.ServerPath = Server.MapPath("~/");
string myPath = Server.MapPath("~/") + filename;
StatusLabel.Text = "Upload status: File uploaded!";
StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
So based on the other threads and suggestions, I thought this might be a permission problem and I went ahead and added any sort of user that I could think of (or were suggested in the threads) to the main folder and re-ran the program -> still the same error.
So then I decided to see if the issue is really with the folder permissions. So I removed the FileUpload.SaveAs and replaced it with the following line:
System.IO.File.WriteAllText(fileHander.GetFullPath(fileNo) + "//test.txt", "Something!");
The program worked with no issues. It did create the test.txt in the right subfolder and populated it with the word "Something!". So now I'm thinking it can NOT be a folder permission issue, but if not, then what is the issue and also why do I get the no
access error message.
Any help is appreciated.
Jul 22, 2015 10:23 PM|Li Wang|LINK
Thank you for your post.
SaveAs method need a file name as parameter. But fileHander.GetFullPath(fileNo) seems a folder name. Please double check it and change to a file name.