2017-04-19 1 views
-2

이 내가 간단한 위해서 var_dump를 가지고 내 PHP 파일에vb.net 후 이미지 파일은 0

Dim Path As String = "C:\dir\temp\" 
    Dim fileLocation As String = Path + "FacePictureL.Jpeg" 
    Dim fileLocation2 As String = Path + "LH1.Jpeg" 
    Dim values As New NameValueCollection() 
    Dim files As New NameValueCollection() 
    values.Add("load", "OnPost") 
    values.Add("value2", "value2") 
    files.Add("FacePictureL", fileLocation) 
    files.Add("LH1", fileLocation2) 
    Dim sendData As New DataUpload() 
    Dim response = sendData.sendHttpRequest("http://127.0.0.1/php/test/index.php", values, files) 
    MsgBox(response) 
    TextBox1.Text = response 

하여 전화

Public Function sendHttpRequest(url As String, values As NameValueCollection, Optional files As NameValueCollection = Nothing) As String 
    Dim boundary As String = "----------------------------" + DateTime.Now.Ticks.ToString("x") 

    Dim boundaryBytes As Byte() = System.Text.Encoding.UTF8.GetBytes((Convert.ToString(vbCr & vbLf & "--") & boundary) + vbCr & vbLf) 

    Dim trailer As Byte() = System.Text.Encoding.UTF8.GetBytes((Convert.ToString(vbCr & vbLf & "--") & boundary) + "--" & vbCr & vbLf) 

    Dim boundaryBytesF As Byte() = System.Text.Encoding.ASCII.GetBytes((Convert.ToString("--") & boundary) + vbCr & vbLf) 

    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) 
    request.ContentType = Convert.ToString("multipart/form-data; boundary=") & boundary 
    request.Method = "POST" 
    request.KeepAlive = True 
    request.Credentials = System.Net.CredentialCache.DefaultCredentials 

    Dim requestStream As Stream = request.GetRequestStream() 

    For Each key As String In values.Keys 

     Dim cntDisp = "Content-Disposition: form-data; name=""{0}"";" & vbCr & vbLf & vbCr & vbLf & "{1}" 
     Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(String.Format(cntDisp, key, values(key))) 
     requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
     requestStream.Write(formItemBytes, 0, formItemBytes.Length) 
    Next 

    If files IsNot Nothing Then 
     For Each key As String In files.Keys 
      If File.Exists(files(key)) Then 
       Dim bytesRead As Integer = 0 
       Dim buffer As Byte() = New Byte(2047) {} 
       Dim cnt As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & vbCr & vbLf 
       Dim cntype As String = "Content-Type: application/octet-stream" & vbCr & vbLf & vbCr & vbLf 
       Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(String.Format(cnt & cntype, key, files(key))) 
       requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
       requestStream.Write(formItemBytes, 0, formItemBytes.Length) 

       Using fileStream As New FileStream(files(key), FileMode.Open, FileAccess.Read) 
        While (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) <> 0 

         requestStream.Write(buffer, 0, bytesRead) 
        End While 

        fileStream.Close() 
       End Using 
      End If 
     Next 
    End If 


    requestStream.Write(trailer, 0, trailer.Length) 
    requestStream.Close() 

    Dim reader As New StreamReader(request.GetResponse().GetResponseStream()) 


    res = reader.ReadToEnd() 

    Return res 

End Function 

포스트를 ($을 할 수있는 기능입니다 _POST) 및 var_dump ($ _ Files)

게시물이 잘 만들어지고 이미지 크기가 항상 0입니다. Content-Type을 변경하려고하지만 동일한 결과가 나타나므로 여기에 집어 넣으십시오. 제안 사항을 보내 주시겠습니까? , 고맙습니다.

+0

왜 C# 태그를 추가 했습니까? – mok

+0

이 함수는 C# i에서 VB로 변환 된 것입니다. 이 문제는 변환 오류 일 수 있으며 누군가 나를 안내 할 수 있다고 가정합니다. 문안 인사 –

답변

0

마침내 나는 몇 일간의 실수와 연구 끝에이 답변을 얻었습니다. (단지 부정적인 평판을주는 것보다이 게시물을 만들기 전에 기억하십시오 ... 저와 같은 사람들은 지금 배우려고 노력하고 있습니다. 일부 고급 도움말) 이것은 VB에서 파일로 게시물을 작성하는 업데이트 된 코드입니다. 실제 호출을 사용하면 다른 사람에게 도움이 될 것입니다.

Imports System.Collections.Specialized 
Imports System.IO 
Imports System.Net 

Public Class DataUpload 
Dim res As String 
Public Function sendHttpRequest(url As String, values As 
NameValueCollection, Optional files As NameValueCollection = Nothing) As 
String 
    Dim boundary As String = "----------------------------" + 
    DateTime.Now.Ticks.ToString("x") 
    Dim boundaryBytes As Byte() = System.Text.Encoding.UTF8.GetBytes((Convert.ToString(vbCr & vbLf & "--") & boundary) + vbCr & vbLf) 
    Dim trailer As Byte() = System.Text.Encoding.UTF8.GetBytes((Convert.ToString(vbCr & vbLf & "--") & boundary) + "--" & vbCr & vbLf) 
    Dim boundaryBytesF As Byte() = System.Text.Encoding.ASCII.GetBytes((Convert.ToString("--") & boundary) + vbCr & vbLf) 
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) 
    request.ContentType = Convert.ToString("multipart/form-data; boundary=") & boundary 
    request.Method = "POST" 
    request.KeepAlive = True 
    request.Credentials = System.Net.CredentialCache.DefaultCredentials 
    Dim requestStream As Stream = request.GetRequestStream() 

    For Each key As String In values.Keys 
     Dim cntDisp = "Content-Disposition: form-data; name=""{0}"";" & vbCr & vbLf & vbCr & vbLf & "{1}" 
     Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(String.Format(cntDisp, key, values(key))) 
     requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
     requestStream.Write(formItemBytes, 0, formItemBytes.Length) 
    Next 
    Dim count = 0 
    If files IsNot Nothing Then 

     For Each key As String In files.Keys 
      If File.Exists(files(key)) Then 
       Dim bytesRead As Integer = 0 
       Dim buffer As Byte() = New Byte(2048) {} 
       Dim cnt As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & vbCr & vbLf 
       Dim cntype As String = "Content-Type: application/octet-stream" & vbCr & vbLf & vbCr & vbLf 
       Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(String.Format(cnt & cntype, key, files(key))) 
       requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 

       requestStream.Write(formItemBytes, 0, formItemBytes.Length) 

       Using fileStream As New FileStream(files(key), FileMode.Open, FileAccess.Read) 
        While (BytesDataHelper(bytesRead, fileStream.Read(buffer, 0, buffer.Length))) <> 0 
         requestStream.Write(buffer, 0, bytesRead) 
        End While 
       End Using 
      End If 
     Next 
    End If 
    requestStream.Write(trailer, 0, trailer.Length) 
    requestStream.Close() 
    Dim reader As New StreamReader(request.GetResponse().GetResponseStream()) 
    res = reader.ReadToEnd() 
    Return res 

End Function 
Private Shared Function BytesDataHelper(Of T)(ByRef target As T, ByVal value As T) As T 
    target = value 
    Return value 
End Function 
End Class