0

我在这里有一个简单的程序,我可以在其中创建一个文件夹,然后将图像保存到创建的文件夹中。该文件夹已成功创建,但在将其保存到新创建的文件时出现错误。错误是:

无法上传文件。发生以下错误:“N:/Kim's New Project/Safety Accident Report/File Uploader 2/File Uploader 2/Uploads/asdsa”是物理路径,但应为虚拟路径。

你能检查我的代码吗?请帮忙。谢谢你。

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;
                }
            }
4

2 回答 2

1

代替

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

尝试

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

您已经知道物理保存路径 - 不需要 Server.MapPath

于 2013-07-12T10:55:43.280 回答
0

尝试..

string strpath = Server.MapPath("~/Test");
if (!(Directory.Exists(strpath)))
{
    Directory.CreateDirectory(strpath);               
}
于 2013-07-12T10:59:54.100 回答