0
B4A (B4X)를 사용하여 Android 에뮬레이터에서 내 PC의 IIS (localhost) 웹 서버로 파일을 업로드합니다 (업로드되는 파일은 .jpg 형식 임). 업로드 루틴은 다음과 같습니다.업로드 된 이미지 파일 B4A를 사용하는 Android가 손상되지 않았습니다.
Dim boundary As String = "---------------------------1461124740692"
Dim stream As OutputStream
stream.InitializeToBytesArray(0)
Dim b() As Byte
Dim eol As String = Chr(13) & Chr(10)
If NameValues <> Null And NameValues.IsInitialized Then
For Each key As String In NameValues.Keys
Dim value As String = NameValues.Get(key)
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${key}"
${value}
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Next
End If
If Files <> Null And Files.IsInitialized Then
For Each fd As MultipartFileData In Files
Dim s As String = _
$"--${boundary}
Content-Disposition: form-data; name="${fd.KeyName}"; filename="${fd.FileName}"
Content-Type: ${fd.ContentType}
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
Dim in As InputStream = File.OpenInput(fd.Dir, fd.FileName)
File.Copy2(in, stream)
stream.WriteBytes(eol.GetBytes("utf8"), 0, 2)
Next
End If
s = _
$"
--${boundary}--
"$
b = s.Replace(CRLF, eol).GetBytes("UTF8")
stream.WriteBytes(b, 0, b.Length)
PostBytes(Link, stream.ToBytesArray)
req.SetContentType("multipart/form-data; boundary=" & boundary)
req.SetContentEncoding("UTF8")
B4A 쪽이 맞습니다. 질문은 ASP 측에 관한 것입니다. 이미지 파일 "test.jpg"는 크기가 18kb 인 루트 폴더에 있지만 파일을 두 번 클릭하고 Windows 뷰어 또는 MSPaint 또는 파일이 비어 있거나 손상되었다고 여는 것으로 표시됩니다. 내 Page_Load 코드는 다음과 같습니다.
Dim length As Integer = Convert.ToInt32(Context.Request.InputStream.Length)
Dim buffer() As Byte = New Byte((length) - 1) {}
Context.Request.InputStream.Read(buffer, 0, length)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim file As New FileStream(Server.MapPath("test.jpg"), FileMode.Create, FileAccess.Write)
ms.WriteTo(file)
file.Close()
Dim target As Bitmap = New Bitmap(CType("10",Integer), CType("10",Integer))
Dim graphics As Graphics = Graphics.FromImage(target)
target.Save(ms, ImageFormat.Jpeg)
ms.Close
Dim js As JavaScriptSerializer = New JavaScriptSerializer
Context.Response.Expires = -1
Context.Response.ContentType = "application/json"
Context.Response.Write(js.Serialize("test response"))
'Context.Response.End
Context.Response.OutputStream.Close
여기에 잘못된 것이 있습니까?