본문 바로가기

자료

[C# / vb.net] winhttp 그누보드 파일 업로드

728x90

 

    Public Function FileUpload(ByVal FilePath As String) As String

        Dim FileName As String, host As String = "program1472.com"

        FileName = Split(FilePath, "\")(UBound(Split(FilePath, "\")))

        Dim fileExt As String = Split(FileName, ".")(Split(FileName, ".").Length - 1)

        Dim V() As String = Split(FileName, ".")

        V(V.Length - 1) = ""

        FileName = Join(V, ".")

        FileName &= (DateTime.Now - New DateTime(2019, 3, 1)).TotalMilliseconds * 10000

        FileName &= "." & fileExt

        Dim Cookie As String = ""

        Dim Header1 As String, Header2 As String

        Dim BHeader1() As Byte, BHeader2() As Byte, Buffer() As Byte

        Dim boundary As String = "167962534731431"

        Dim Content_Type As String = ""

        'Select Case fileExt

        '    Case "xls" : Content_Type = "application/vnd.ms-excel"

        '    Case "xlsx" : Content_Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

        '    Case "xlsm" : Content_Type = "application/vnd.ms-excel.sheet.macroEnabled"  '// .12"

        '    Case "pdf" : Content_Type = "application/pdf"

        '    Case "doc" : Content_Type = "application/msword"

        '    Case "docx" : Content_Type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

        '    Case "docm" : Content_Type = "application/vnd.ms-word.document.macroEnabled"  '// .12"

        '    Case "ppt" : Content_Type = "application/vnd.ms-powerpoint"

        '    Case "pptx" : Content_Type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"

        '    Case "pptm" : Content_Type = "application/vnd.ms-powerpoint.presentation.macroEnabled"  '// .12"

        '    Case Else : Return False

        'End Select

 

        Dim T As String

        Header1 = "-----------------------------" & boundary & "" & vbCrLf

        Header1 &= "Content-Disposition: form-data; name=""upfile""; filename=""" & APP_USER & "." & FileName & """" & vbCrLf

        Header1 &= "Content-Type: " & Content_Type & vbCrLf & vbCrLf

 

        Header2 = vbCrLf & "-----------------------------" & boundary & "--" & vbCrLf

 

        Dim ImageBinary() As Byte

        '### 이미지 바이너리를 얻는다.

        ImageBinary = IO.File.ReadAllBytes(FilePath)

 

        '### 전송할 데이터를 만든다.

        BHeader1 = System.Text.Encoding.UTF8.GetBytes(Header1)

        BHeader2 = System.Text.Encoding.UTF8.GetBytes(Header2)

 

        Dim DS As New MemoryStream

        DS.Write(BHeader1, 0, BHeader1.Length)

        DS.Write(ImageBinary, 0, ImageBinary.Length)

        DS.Write(BHeader2, 0, BHeader2.Length)

 

        DS.Position = 0

        ReDim Buffer(DS.Length)

        DS.Read(Buffer, 0, Buffer.Length) : DS.Close()

        Try

            With CreateObject("WinHttp.WinHttpRequest.5.1")

                .Open("POST", "http://" & host & "/apps/upload.php")

                .SetRequestHeader("Host", host)

                .SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0")

                .SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")

                .SetRequestHeader("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3")

                .SetRequestHeader("Referer", "http://" & host & "/apps/file_upload.php")

                .SetRequestHeader("Content-Type", "multipart/form-data; boundary=---------------------------" & boundary & "")

                .SetRequestHeader("Content-Length", Buffer.Length)

                .SetRequestHeader("Connection", "keep-alive")

                If Len(Cookie) Then .SetRequestHeader("Cookie", Cookie)

                .SetRequestHeader("Upgrade-Insecure-Requests", "1")

                .Send(Buffer)

                .WaitForResponse() : Application.DoEvents()

                T = System.Text.Encoding.Default.GetString(.ResponseBody)

                'T = .ResponseText

                Clipboard.SetText(T)

            End With

            Return T

        Catch ex As Exception

 

        End Try

        Return Nothing

    End Function
728x90