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 に答える