div. Änderungen
This commit is contained in:
@@ -1656,6 +1656,92 @@ Public Class cFormularManager
|
||||
Return ""
|
||||
End Function
|
||||
|
||||
Public Shared Function getFile_FromURLStream_NEW(URL As String, Optional filename As String = "", Optional targetPath As String = "", Optional showError As Boolean = True) As String
|
||||
Try
|
||||
Using webClient As New Net.WebClient()
|
||||
|
||||
' 🟡 Simulate browser (important for protected systems)
|
||||
webClient.Headers.Add("User-Agent", "Mozilla/5.0")
|
||||
webClient.Headers.Add("Accept", "*/*")
|
||||
|
||||
' Download data
|
||||
Dim data() As Byte = webClient.DownloadData(URL)
|
||||
|
||||
' Read Content-Type header safely
|
||||
Dim contentType As String = ""
|
||||
If webClient.ResponseHeaders IsNot Nothing AndAlso webClient.ResponseHeaders("Content-Type") IsNot Nothing Then
|
||||
contentType = webClient.ResponseHeaders("Content-Type").ToLower()
|
||||
End If
|
||||
|
||||
' Try to get filename from headers if not provided
|
||||
If String.IsNullOrEmpty(filename) Then
|
||||
Dim contentDisposition = webClient.ResponseHeaders("Content-Disposition")
|
||||
If contentDisposition IsNot Nothing AndAlso contentDisposition.Contains("filename=") Then
|
||||
filename = contentDisposition.Split("filename=")(1).Replace("""", "").Trim()
|
||||
filename = System.IO.Path.GetFileNameWithoutExtension(filename)
|
||||
Else
|
||||
filename = Guid.NewGuid().ToString()
|
||||
End If
|
||||
End If
|
||||
|
||||
' Determine extension
|
||||
Dim extension As String = ""
|
||||
|
||||
If contentType.Contains("pdf") Then
|
||||
extension = ".pdf"
|
||||
|
||||
ElseIf contentType.Contains("spreadsheetml") Then
|
||||
extension = ".xlsx"
|
||||
|
||||
ElseIf contentType.Contains("ms-excel") Then
|
||||
extension = ".xls"
|
||||
|
||||
ElseIf contentType.Contains("json") Then
|
||||
extension = ".json"
|
||||
|
||||
ElseIf contentType.Contains("html") Then
|
||||
extension = ".html" ' ⚠️ likely login page instead of file
|
||||
End If
|
||||
|
||||
' Fallback: detect PDF by magic number
|
||||
If extension = "" AndAlso data.Length > 4 Then
|
||||
If data(0) = &H25 AndAlso data(1) = &H50 AndAlso data(2) = &H44 AndAlso data(3) = &H46 Then
|
||||
extension = ".pdf"
|
||||
End If
|
||||
End If
|
||||
|
||||
' Default fallback
|
||||
If extension = "" Then extension = ".bin"
|
||||
|
||||
' Build path
|
||||
Dim fullPath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename & extension)
|
||||
|
||||
' Save file
|
||||
If extension = ".pdf" Then
|
||||
Using stream As New MemoryStream(data)
|
||||
Dim doc As New Spire.Pdf.PdfDocument()
|
||||
doc.LoadFromStream(stream)
|
||||
doc.SaveToFile(fullPath)
|
||||
End Using
|
||||
|
||||
Else
|
||||
System.IO.File.WriteAllBytes(fullPath, data)
|
||||
End If
|
||||
|
||||
targetPath = fullPath
|
||||
Return targetPath
|
||||
|
||||
End Using
|
||||
|
||||
Catch ex As Exception
|
||||
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace,
|
||||
System.Reflection.MethodInfo.GetCurrentMethod.Name,
|
||||
If(Not showError, "LOG", ""))
|
||||
End Try
|
||||
|
||||
Return ""
|
||||
End Function
|
||||
|
||||
Public Shared Sub PrintViaGS(PDFFile As String, printerName As String)
|
||||
Try
|
||||
Dim assembly = System.Reflection.Assembly.GetExecutingAssembly()
|
||||
|
||||
Reference in New Issue
Block a user