2014-03-14 16 views
0

我需要在另一個文件夾(共2個文件夾)內創建一個文件夾,因爲我需要插入圖像。如何通過在asp.net中創建文件夾來插入圖像?

protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
{ 



    AjaxControlToolkit.AsyncFileUpload async = (AjaxControlToolkit.AsyncFileUpload)sender; 

    if (async.HasFile) 
    { 


     if (image1 != null) 
     { 
      try 
      { 
       File.Delete(MapPath("~/images/" + SelectHotels.SelectedItem) + image1); 
      } 
      catch 
      { 
      } 
     } 
     image1 = e.FileName; 
     if (File.Exists(MapPath("~/images/" + SelectHotels.SelectedItem + "/") + image1)) 
     { 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Change Image", "alert('Image with same name already exit please change name and upload');", true); 
      image1 = null; 

     } 
     else 
     { 
      if (!Directory.Exists(Server.MapPath("~/images/" + SelectHotels.SelectedItem))) 
      { 
       Directory.CreateDirectory(Server.MapPath("~/images/" + SelectHotels.SelectedItem)+image1); 

       str = "~/images/" + SelectHotels.SelectedItem; 

       str1 = "~/images/" + SelectHotels.SelectedItem + "/" + image1; 

      } 
+0

[關注這個帖子,希望這將有助於] [1] [1]:http://stackoverflow.com/questions/3561106/ Ajax的工具包-asyncfileupload - 不工作-內部-A-正常面板 – UmarKashmiri

回答

0

請試試這個代碼,

public void EnsureDirectoriesExist() 
    { 

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

    } 

    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(@"~/Folder_Name/" + FileUpload1.FileName); 
       FileUpload1.SaveAs(filePath); 


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


    } 
相關問題