0

使用 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

这里看起来有什么不对吗?

4

1 回答 1

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
于 2017-01-14T01:28:21.200 回答