Android Question HTTP Post Requests to .NET Web API

Ohanian

Active Member
Licensed User
Longtime User
Hi,

here's a piece of one of my projects :
.Net :

B4X:
  <WebMethod()> _
    Public Function ProfileUploader() As String
        Try
            Dim fileName As String = Guid.NewGuid.ToString.ToLower

            Dim length As Integer = CInt(Context.Request.InputStream.Length)
            Dim buffer As Byte() = New Byte(length - 1) {}
            Context.Request.InputStream.Read(buffer, 0, length)

            Dim ms As New MemoryStream(buffer)

            GenerateThumbnails(1, ms, System.Web.Hosting.HostingEnvironment.MapPath("~/ImageStorage/") & fileName)

            'Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/ImageStorage/") & fileName, FileMode.Create)
            'ms.WriteTo(fs)
            ms.Close()
            'fs.Close()
            'fs.Dispose()

            Dim js As New JavaScriptSerializer()

            Context.Response.Expires = -1
            Context.Response.ContentType = "application/json"

            Context.Response.Write(js.Serialize(fileName))

            Try
                Context.Response.End()
            Catch ex1 As Threading.ThreadAbortException
            End Try

        Catch ex As Exception
            Return String.Empty
        End Try
    End Function

    Private Sub GenerateThumbnails(ByVal scaleFactor As Double, ByVal sourcePath As Stream, ByVal targetPath As String)
        Dim image = Drawing.Image.FromStream(sourcePath)
        Dim newWidth = CType((image.Width * scaleFactor), Integer)
        Dim newHeight = CType((image.Height * scaleFactor), Integer)
        Dim thumbnailImg = New Drawing.Bitmap(newWidth, newHeight)
        Dim thumbGraph = Drawing.Graphics.FromImage(thumbnailImg)
        thumbGraph.CompositingQuality = CompositingQuality.HighQuality
        thumbGraph.SmoothingMode = SmoothingMode.HighQuality
        thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic
        Dim imageRectangle = New Drawing.Rectangle(0, 0, newWidth, newHeight)
        thumbGraph.DrawImage(image, imageRectangle)
        thumbnailImg.Save(targetPath, image.RawFormat)
    End Sub

b4a :

B4X:
            Dim myJob As HttpJob                   
            myJob.Initialize("PostImage", Me)   
            myJob.PostFile("http://SITE.com/webservice.asmx/ProfileUploader", File.DirRootExternal, "profile.jpg")
 
Upvote 0

echapeta

Member
Licensed User
Longtime User
Hello Ohanian,

In .NET , is Web API 2.0 , MVC , or ?

Here is the code I'm using:

Public Sub NGReport()
Trace(" > NG Report Multi File: " & HttpContext.Current.Request.Files.Count)
If HttpContext.Current.Request.Files.AllKeys.Any() Then

Dim Li As String() = HttpContext.Current.Request.Files.AllKeys
For F = 0 To Li.Count - 1

Dim httpPF As HttpPostedFile = HttpContext.Current.Request.Files(F)

Trace(" File " & httpPF.FileName & " / " & HttpContext.Current.Request.Files.Keys(F))
Dim FN As String = "C:\NG911\" & httpPF.FileName.Trim

Trace(" File: " & FN)

httpPF.SaveAs(FN)
Next
End If

Trace(" > Variables:")
For Each key As String In HttpContext.Current.Request.Form.AllKeys
Dim value As String = HttpContext.Current.Request.Form(key)
Trace(" Pair: " & key & " / " & value)
Next

End Sub


Thank You.
 
Upvote 0
Top