I am trying to create directories and upload files to the server, but it won't give me access to. This is my code:
if (FileUploadControl.HasFile)
{
string path = "~/MSImages/";
string mappath = Server.MapPath(path);
if (FileUploadControl.PostedFile.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
string extension = FileUploadControl.PostedFile.FileName;
extension = extension.Substring(extension.LastIndexOf('.'));
if (!Directory.Exists(mappath))
Directory.CreateDirectory(mappath);
string filename = imgext + Request.QueryString["id"];
FileUploadControl.SaveAs(mappath + filename + extension);
}
}
It works when done on my local computer, but not when on my host. How to fix this?
Solutions1
This is most likely a permission issue. Your IIS account user doesn't have the appropriate permissions to change the resource on the other machine.
You could change the IIS user to a low-permission domain user that is just granted access to that specific machine's share. That should fix your issue.
Talk1:
If I don't have access to the IIS settings, can I connect to the FTP somehow in C# and create directories/files?
Talk2:
If you have the appropriate permissions: yes.
Talk3:
I have access to the FTP server and I can add/remove any file I want in there.
Talk4:
And by the way, you don't need to change anything in IIS. You just have to grant the IIS user access to that share. If you can do that, you are there too.