2016-11-18 28 views
0

我上傳了PDF的數量,並且我在那裏存儲SQL服務器的路徑。 我需要能夠刪除保存在文件夾中的pdf文件。我已經設法讓上傳&下載工作,但刪除令我困惑。 我的代碼是...`刪除上傳的文件形式文件夾和路徑存儲在SQL服務器asp.net

protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 
     SqlConnection con = new SqlConnection(cs); 
     ProductID = gridView.DataKeys[e.RowIndex].Values["ProductID"].ToString(); 
     //File Delete start// 
     using (SqlConnection con1 = new SqlConnection(cs)) 
     { 
      SqlCommand cmd1 = new SqlCommand("SELECT PDF1, PDF2, PDF3 FROM Product WHERE ProductID = @ProductID", con1); 
      cmd1.Parameters.AddWithValue("ProductID", ProductID); 

      SqlDataAdapter sda = new SqlDataAdapter(cmd1); 
      DataTable dt = new DataTable(); 
      sda.Fill(dt); 

      try 
      { 
       string pdf1 = dt.Rows[0]["PDF1"].ToString(); 
       string pdf2 = dt.Rows[0]["PDF2"].ToString(); 
       string pdf3 = dt.Rows[0]["PDF3"].ToString(); 

       string strFileFullPath1 = VirtualPathUtility.ToAbsolute(pdf1); 
       if (File.Exists(strFileFullPath1)) 
       { 
        File.Delete(strFileFullPath1); 
       } 
       string strFileFullPath2 = VirtualPathUtility.ToAbsolute(pdf2); 
       if (File.Exists(strFileFullPath2)) 
       { 
        File.Delete(strFileFullPath2); 
       } 
       string strFileFullPath3 = VirtualPathUtility.ToAbsolute(pdf3); 
       if (File.Exists(strFileFullPath3)) 
       { 
        File.Delete(strFileFullPath3); 
       } 
      } 
+2

你爲什麼包括一個鏈接到你的代碼的圖片,而不是僅僅把代碼本身?請解決這個問題。 –

+0

我附上一段代碼圖片 –

+1

我知道你做過。你爲什麼不把代碼放在問題本身?我們不想點擊一個鏈接來獲取您的代碼,讓我們看看它的圖片而不是代碼本身。請修改您的問題,以便在問題的正文中包含代碼本身。 –

回答

1

從相對路徑得到的絕對路徑,然後刪除該文件

string strFileFullPath= Server.MapPath(brangimage); 
if (System.IO.File.Exists(strFileFullPath)) { 
     System.IO.File.Delete(strFileFullPath); 
    } 
+0

它沒有執行,如果語句,但路徑是存在的。 –

+0

你是否使用上面的代碼得到任何異常?檢查它從地圖路徑返回的路徑,並通過直接從資源管理器 – Damith

+0

打開它手動檢查否,上面的代碼沒有例外。 (ex.Message =「相對虛擬路徑」C:/ Users/masum/Documents/Visual Studio 2015/MapleAutomation/MapleAutomation/Content/Data/document.pdf'不允許在這裏。「) –

相關問題