在ASP.NET中创建文件夹并将图像上传到该文件夹​​的最佳方法?

Eti*_*nne 3 c# vb.net asp.net image

关于如何创建文件夹的任何代码示例在我的root中说"pics",然后将文件上传控件中的图像上传到"pics"文件夹中?如果你不想给我所有的代码,我会很高兴有一个很好的链接也告诉我如何在VB.NET中完成(C#也没关系).

rom*_*n m 13

尝试/抓住你就是:)

public void EnsureDirectoriesExist()
        {

                // if the \pix directory doesn't exist - create it. 
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/pix/")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/pix/"));
                }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {


                if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".jpg")
                {
                    // create posted file
                    // make sure we have a place for the file in the directory structure
                    EnsureDirectoriesExist();
                    String filePath = Server.MapPath(@"~/pix/" + FileUpload1.FileName);
                    FileUpload1.SaveAs(filePath);


                }
                else
                {
                    lblMessage.Text = "Not a jpg file";
                }


        }
Run Code Online (Sandbox Code Playgroud)