我试图让图片上传工作。到目前为止,我有这个:
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?
图像位于不同的文件夹中,因此我需要修剪/ images
后面的所有内容答案 0 :(得分:1)
所以你要删除静态&#34; C:/ website&#34;从路径?
然后你可以简单地使用
If strPath.StartsWith("C:/website") Then
txtSmallDealOneImg.Text = strPath.Substring("C:/website".Length)
End If
如果您想删除Image-subdirectory之前的所有内容,可以使用:
Dim imageIndex = strPath.IndexOf("/Images/", StringComparison.InvariantCultureIgnoreCase)
If imageIndex >= 0 Then
txtSmallDealOneImg.Text = strPath.Substring(imageIndex)
End If
答案 1 :(得分: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