I made a private image hosting website using chevereto, i have bought it, and I need it to upload images directly with api. I have the code bellow that works only with images that are 1 mb or smaller, arroud 1 mb don't know the exact size. I get (400) bad request and the error_log of the website says: Empty upload source. I guess is the streamWriter but I really don't know why. I did check Max upload size, Max post size, Memory Limit and Max execution time and their values are 1024mb, 1024mb, 1024mb, 300s.
Private Function PostImage(ByVal FilePath As String) As String
Try
Dim Link As String = String.Empty
Dim Stream As System.IO.Stream = File.OpenRead(FilePath)
Dim NumArray(CInt(Stream.Length) + 1) As Byte
Stream.Read(NumArray, 0, CInt(NumArray.Length))
Stream.Close()
Dim num As Integer = 32767
Dim base64String As String = Convert.ToBase64String(NumArray)
Dim stringBuilder As System.Text.StringBuilder = New System.Text.StringBuilder()
Dim num1 As Integer = 0
While num1 < base64String.Length
stringBuilder.Append(Uri.EscapeDataString(base64String.Substring(num1, Math.Min(num, base64String.Length - num1))))
num1 = num1 + num
End While
Dim str As String = String.Concat("source=", stringBuilder.ToString())
Dim httpWebRequest As System.Net.HttpWebRequest = DirectCast(WebRequest.Create("http://%mywebsite.com%/api/1/upload/?key=%myapikey%"), System.Net.HttpWebRequest)
httpWebRequest.Method = "POST"
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
httpWebRequest.Accept = "application/xml"
httpWebRequest.ServicePoint.Expect100Continue = False
Dim streamWriter As New System.IO.StreamWriter(httpWebRequest.GetRequestStream())
streamWriter.Write(str)
streamWriter.Close()
Dim responseStream As System.IO.Stream = httpWebRequest.GetResponse().GetResponseStream()
Link = New StreamReader(responseStream).ReadToEnd()
Link = Split(Link, "width")(2)
Link = Split(Link, """url"":""")(1)
Link = Split(Link, """}")(0)
Link = Replace(Link, "\/", "/")
Link = Split(Link, """,""")(0)
Return Link
Catch ex As Exception
EnterError("Couldn't Post Image, " & ex.Message & " File: " & FilePath)
Return ex.Message
End Try
End Function
PS: I know the link can be taken much better but I was lazy
What I have tried:
Searching for the same or similar problem without much success. Tried using a direct image to base64 but that only made it not work at all. This is the base64 function is used:
Using MemoryStream As New MemoryStream
Image.Save(MemoryStream, ImageFormat)
Dim Result As String = Convert.ToBase64String(MemoryStream.ToArray())
MemoryStream.Close()
Return Result
End Using