【问题标题】:Best way to create a folder and upload a image to that folder in ASP.NET?在 ASP.NET 中创建文件夹并将图像上传到该文件夹​​的最佳方法?
【发布时间】:2010-10-18 22:15:52
【问题描述】:

关于我必须如何在我的根目录中创建一个名为“pics”的文件夹,然后将文件上传控件中的图像上传到该“pics”文件夹中的任何代码示例? 如果你不想给我所有的代码,我会很高兴有一个很好的链接来告诉我这将如何在 VB.NET 中完成(C# 也可以)。

【问题讨论】:

    标签: c# asp.net vb.net image


    【解决方案1】:

    Try/Catch 就在你身上 :)

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

    【讨论】:

    • +1 表示努力。鉴于最近关于过度工程的讨论,我倾向于将 EnsureDirectoriesExist() 的提取视为不需要抽象的示例。当您考虑上下文(stackoverflow 答案)时,我认为可以肯定地说,您不需要它。
    • thx, EnsureDirectoriesExist() 也可以用于其他文件系统操作,而不仅仅是图像上传
    【解决方案2】:

    我会这样做。

        protected void OnUpload_Click(object sender, EventArgs e)
        {
            var path = Server.MapPath("~/pics");
            var directory = new DirectoryInfo(path);
    
            if (directory.Exists == false)
            {
                directory.Create();
            }
    
            var file = Path.Combine(path, upload.FileName);
    
            upload.SaveAs(file);
        }
    

    【讨论】:

      【解决方案3】:

      在文件夹内创建文件夹并上传文件

          DirectoryInfo info = new DirectoryInfo(Server.MapPath(string.Format("~/FolderName/") + txtNewmrNo.Text)); //Creating SubFolder inside the Folder with Name(which is provide by User).
          string directoryPath = info+"/".ToString();
          if (!info.Exists)  //Checking If Not Exist.
          {
              info.Create();
              HttpFileCollection hfc = Request.Files;
              for (int i = 0; i < hfc.Count; i++) //Checking how many files in File Upload control.
              {
                  HttpPostedFile hpf = hfc[i];
                  if (hpf.ContentLength > 0)
                  {
                      hpf.SaveAs(directoryPath + Path.GetFileName(hpf.FileName)); //Uploading Multiple Files into newly created Folder (One by One).
                  }
              }
          }
          else
          {
              ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('This Folder already Created.');", true);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 2012-06-19
        • 1970-01-01
        相关资源
        最近更新 更多