问题描述:

I have a simple program here wherein I can create a folder then save the an image to the folder created. The folder gets successfully created but I'm getting error into saving it to the newly created file. The error is:

The file could not be uploaded. The following error occured: 'N:/Kim's New Project/Safety Accident Report/File Uploader 2/File

Uploader 2/Uploads/asdsa' is a physical path, but a virtual path was

expected.

Can you please check my code. Please help. Thank you.

protected void button1_Click(object sender, EventArgs e)

{

if (FileUpload2.HasFile)

{

try

{

if (FileUpload2.PostedFile.ContentType == "image/jpeg")

{

if (FileUpload2.PostedFile.ContentLength < 512000)

{

string strpath = @"N:\Kim's New Project\Safety Accident Report\File Uploader 2\File Uploader 2\Uploads\" + txtName.Text;

if (!(Directory.Exists(strpath)))

{

Directory.CreateDirectory(strpath);

lblResult.Text = "Directory Created";

if ((Directory.Exists(strpath)))

{

string filename = Path.GetFileName(FileUpload2.FileName);

FileUpload2.SaveAs(Server.MapPath(strpath) + filename);

Label1.Text = "File uploaded successfully!";

}

}

else

{

lblResult.Text = "Already Directory Exists with the same name";

}

}

else

Label1.Text = "File maximum size is 500 Kb";

}

else

Label1.Text = "Only JPEG files are accepted!";

}

catch (Exception exc)

{

Label1.Text = "The file could not be uploaded. The following error occured: " + exc.Message;

}

}

网友答案:

Instead of

FileUpload2.SaveAs(Server.MapPath(strpath) + filename);

try

FileUpload2.SaveAs(Path.Combine(strPath, filename));

you already know the physical save path - no need for Server.MapPath

网友答案:

Try..

string strpath = Server.MapPath("~/Test");
if (!(Directory.Exists(strpath)))
{
    Directory.CreateDirectory(strpath);               
}
相关阅读:
Top