views:

691

answers:

2

I can't save a file on my webserver with FileUpload.SaveAs() or with File.WriteAllText(). The folder I save in is CHMOD 666.

My code is this:

File.WriteAllText(MapPath(".") + "\\Ads\\test.jpg", "test");

This code works perfectly in Dev. server.

Best regards, Lasse Espeholt

System.UnauthorizedAccessException: Access to the path *' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.UI.WebControls.FileUpload.SaveAs(String filename) at *(Object sender, ListViewInsertEventArgs e)

A: 

Your error message states:

System.UnauthorizedAccessException: Access to the path *' is denied.

This looks a little odd. Have you edited the path or is that exactly how it's displayed? If the latter then something is going wrong with your call to MapPath.

I know the following may seem obvious, but I've been bitten by assumptions too many times in the past.

Have you verified that the folder you think you're writing to is the one that MapPath(".") + "\\Ads\\test.jpg" actually resolves to?

Do you have full rights to the "Ads" sub folder as well?

ChrisF
I hided the path for security reasons :)
Lasse Espeholt
Fair enough - was it what you expected it to be though?
ChrisF
+3  A: 

You will need to give read write permissions to machine's ASPNET account for desired folder. You can do it by opening folder properties -> security tab.

Sumit Deo
I have used your comment and displayed it to the webadmin. Now it works thanks :)
Lasse Espeholt