Uploaded image file Android using B4A not working - corrupted

1.1k views Asked by At

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). 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. The question is about the ASP side. 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. Here's my Page_Load code:

    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?

1

There are 1 answers

0
swabygw On

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