0
B4A(B4X)を使用して、自分のAndroidエミュレータから自分のPCのIIS(localhost)Webサーバーにファイルをアップロードします(アップロードするファイルは.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
ここで何か間違っていますか?