使用B4A(B4X),我将我的Android模拟器中的文件上传到我的PC上的IIS(localhost)Web服务器(上传的文件是.jpg格式)。这是上传例程:
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 Viewer或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
这里有什么问题吗?
答案 0 :(得分:0)
解决了这个问题:
For x = 0 To Request.Files.Count-1
'objWriter.WriteLine("Inside For Loop ")
Dim s As String = Server.HtmlEncode(Request.Files.AllKeys(x))
Dim f As HttpPostedFile = Request.Files(s)
Dim fname as String = s '"test.jpg"
objWriter.WriteLine("New Filename: " & s)
'Dim fpath = Path.Combine(Server.MapPath("~/App_Data"), fname)
f.SaveAs(Server.MapPath("/App_Data/"&fname))
Next