السلام عليكم ورحمة الله و بركاته
كيف نعالج خطأ بسبب حروف غير واضحة ...اليكم الكود:
كود :
Private Function GetfileName(ByVal aurl As String) As String
Dim n As String = ""
Try
Dim wc2 As New WebClient
wc2.Encoding = Encoding.UTF8
wc2.Headers.Add("cookie", cok)
wc2.DownloadData(aurl)
Dim cd As String = wc2.ResponseHeaders("Content-Disposition").ToString
n = New ContentDisposition(cd).FileName
Return n 'ew ContentDisposition(cd).FileName
'FormatException
Catch ex As FormatException
MsgBox(Err.Description)
Catch ex As Exception
Return Nothing
End Try
End Function
الخطا ان الاسم يظهر بحروف غريبة ونوع الخطأ من نوع FormatException
كيف اعالجه بحيث يستمر البرنامج ولا يتخطى بسبب هذا الخطأ؟
شكر الله لكم جميعا
جرب تغيير UTF8 الى نوع اخر
بصراحة وجدت طريقة افضل من اللتي لدي و حصلت عليها من قوقل قديما
بحثت مرة اخرى لكي انسخ الكود لكن لم يظهر الكود الذي لدي الان لكن اظن انه نفس المؤدى
كود :
Using client As New WebClient()
Dim uri As New Uri("http://example.com/download")
Dim data As Byte() = client.DownloadData(uri)
Dim filename As String = ""
If client.ResponseHeaders("Content-Disposition") IsNot Nothing Then
Dim contentDisposition As String = client.ResponseHeaders("Content-Disposition")
' Parse the contentDisposition string to extract the filename
' For example: attachment; filename="document.pdf"
Dim parts As String() = contentDisposition.Split(";")
For Each part As String In parts
If part.Trim().StartsWith("filename=") Then
filename = part.Trim().Replace("filename=", "").Replace("""", "")
Exit For
End If
Next
End If
' Use the extracted filename or a default one
If String.IsNullOrEmpty(filename) Then
filename = "default_download.bin"
End If
' Save the data to the file
System.IO.File.WriteAllBytes(filename, data)
End Using
هل تريد فقط استخراج filename من headers الرابط؟
(25-10-25, 05:56 AM)Zuhare كتب : [ -> ]هل تريد فقط استخراج filename من headers الرابط؟
موضوعها سهل لكن الكود القديم يواجه مشكلة الترميز ...الان انتهىت المشكلة .
هذا هو الكود بعد ان اوقفت البرنامج ...الكود كان Function كامل اخذته فقط بهذه الصورة.
كود :
Dim fileData As Byte() = wc2.DownloadData(aurl)
Dim contentDisposition As String = wc2.ResponseHeaders("Content-Disposition")
If Not String.IsNullOrEmpty(contentDisposition) Then
Dim fileName As String = ""
If contentDisposition.Contains("filename=") Then
fileName = contentDisposition.Substring(contentDisposition.IndexOf("filename=") + 9)
fileName = fileName.Replace("""", "")
fileName = Uri.UnescapeDataString(fileName)
Return fileName
End If
End If