When I try to apply SaveAs to FileUpload I receive Exception Access to file denied

  • Thread starter Sergey Topychkanov
  • Start date
S

Sergey Topychkanov

When I try to apply SaveAs to FileUpload I receive Exception Access to file
denied when debugging on my local computer code for ASP.NET 3.5. I have
Windows Vista SP1 and Visual Studio 2008 Professional. Code is next

protected void BtnUpload_Click(object sender, EventArgs e)

{

string fileType = "image/pjpeg";

if (PictureFileUpload.PostedFile.ContentLength != 0) {

if (PictureFileUpload.PostedFile.ContentType == fileType)

{





LblContentType.Text =
PictureFileUpload.PostedFile.ContentType;

string destDir = Server.MapPath("");

string fileName = GetFileName();

string destPath = Path.Combine(destDir, fileName);

try

{

PictureFileUpload.SaveAs(destDir);

}

catch (Exception err){

LblContentType.Text = err.Message;

}

}

}

What to do? Please write me directly to (e-mail address removed)
 
J

Jeroen Mostert

Sergey said:
When I try to apply SaveAs to FileUpload I receive Exception Access to
file denied when debugging on my local computer code for ASP.NET 3.5.
string destDir = Server.MapPath("");

string fileName = GetFileName();

string destPath = Path.Combine(destDir, fileName);
Have you checked the contents of "destPath", and verified that you can
actually write to it? You would want uploads to be written in a separate
directory, not the root of your web application. Try creating a directory
"uploads" and using Server.MapPath() on that. (I actually don't know what
'Server.MapPath("")' does, but it can't be good.)
What to do? Please write me directly to (e-mail address removed)
Nah. My mother told me never to mail strange people directly.
 
S

Sergey Topychkanov

My Server.MapPath() is alright. I just cannot share folder in Vista for
Visual Studio operation. I am writing net application.
Help me.
Thanks in advance.
Sergey.
 
D

Dawid Rutyna

My Server.MapPath() is alright. I just cannot share folder in Vista for
Visual Studio operation. I am writing net application.

I don't know how it is on vista but on xp i would try this:

1. Launch Windows Explorer or My Computer.
2. Click on the Tools at the menu bar, then click on Folder Options.
3. Click on View tab.
4. In the Advanced Settings section at the bottom of the list, uncheck and
unselect (clear the tick) on the "Use simple file sharing (Recommended)"
check box.
5. Click OK.
6. Right click on the folder, properties, security
7. Set write rights for ASPNET account
8. Write data to file with this code:
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);
// Close file
newFile.Close();

Dawid Rutyna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top