[英]Uploaded image file Android using B4A not working - corrupted
Using B4A (B4X), I upload a file from my Android emulator to my IIS (localhost) web server on my PC (the file being uploaded is .jpg format). 使用B4A(B4X),我将Android模拟器中的文件上传到PC上的IIS(localhost)Web服务器上(正在上传的文件为.jpg格式)。 Here's the upload routine:
这是上传例程:
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")
The B4A side is correct. B4A端是正确的。 The question is about the ASP side.
问题是关于ASP方面的。 The image file "test.jpg" shows up in the root folder with size 18kb, but double-clicking on it and opening in, say, Windows Viewer or MSPaint or whatever says file is empty or corrupted.
图像文件“ test.jpg”显示在大小为18kb的根文件夹中,但是双击它并打开,例如Windows Viewer或MSPaint或任何显示为空或损坏的文件。 Here's my Page_Load code:
这是我的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
Does anything here look wrong? 这里看起来有问题吗?
Got this sorted out: 得到了解决:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.