2014-10-02 81 views
0

我試圖讓圖片上傳工作。到目前爲止,我有這樣的:如何獲取圖像的路徑並修剪它?

 Dim dlg As New OpenFileDialog 
     Dim strPath As String = "" 
     dlg.Filter = "PNG(*.png;)|*.png;" 
     dlg.ShowDialog() 

     Dim fileName As String = dlg.FileName.Trim 
     Dim ftpDirectory As String = "" 
     If fileName <> "" Then 
      If fileName.Contains("[") AndAlso fileName.Contains("].png") Then 
       MessageBox.Show("The file you have selected is on the FTP", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      Else 

       strPath = dlg.FileName.ToString 

       End If 

       If button = 1 Then 
       txtSmallDealOneImg.Text = strPath 

然而,strPath的返回整個路徑的圖像文件,例如C:/website/Images/page1/photo1.png

如何調整輸出,使其只顯示/Images/Page1/photo1.png?

的圖像是在不同的文件夾,這樣我就需要修剪的一切多數民衆贊成其次/圖像

回答

1

所以要刪除靜態的「C:/網站」從路徑?

然後,你可以簡單地使用

If strPath.StartsWith("C:/website") Then 
    txtSmallDealOneImg.Text = strPath.Substring("C:/website".Length) 
End If 

如果你不是要刪除的圖像,子目錄之前的一切,你可以使用:如果您正在搜索的是除掉一個通用的方法

Dim imageIndex = strPath.IndexOf("/Images/", StringComparison.InvariantCultureIgnoreCase) 
If imageIndex >= 0 Then 
    txtSmallDealOneImg.Text = strPath.Substring(imageIndex) 
End If 
+0

如果圖像不同文件夾ei。 D:/ Other/Images,有沒有什麼辦法讓它更通用,例如在/ iamges – user3845868 2014-10-02 10:29:29

+1

@ user3845868之前修剪任何東西:你沒有在你的問題中提到過,是嗎?什麼是規則,邏輯是什麼?你想從'Images/...'開始的一切嗎? – 2014-10-02 10:30:55

+0

問題被編輯 – 2014-10-02 10:32:35

0

的你的網站的物理根文件夾,你可以嘗試這樣的事情

Public Function GetURLFromAbsolutePath(ByVal absPath As String) As String 
    Dim baseSitePath As String = HttpContext.Current.Server.MapPath("~") 
    Dim url As String = String.Format("{0}", absPath.Replace(baseSitePath, "") _ 
                 .Replace("\", "/")) 

    Return url 
End Function