Click here to Skip to main content
15,887,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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
Posted
Updated 29-Feb-16 7:39am
v2
Comments
ZurdoDev 29-Feb-16 9:41am    
Simple, ask your hoster which setting needs increased.
evolution74 29-Feb-16 9:44am    
all the settings needed have been increased!
ZurdoDev 29-Feb-16 9:53am    
Clearly not if the error still says the file is too big.
evolution74 29-Feb-16 9:54am    
the error doesn't say that
evolution74 29-Feb-16 9:55am    
the error from the error_log is empty upload source

1 solution

The website you're uploading to has to allow content to be that large. I'm assuming an ASP.NET website.

In the Web.config file of your website project, there should be a <system.webServer> configuration item. If there isn't one, add it to the <configuration> tag:
<configuration>
    <system.webserver>
        <security>
          <requestfiltering>
            <requestlimits maxallowedcontentlength="1073741824" />
          </requestfiltering>
        </security>
    </system.webserver>
    ...</configuration>

The example above sets the request content limit at 1GB.
 
Share this answer
 
Comments
evolution74 29-Feb-16 14:19pm    
Assume again. is not ASP.NET
evolution74 29-Feb-16 14:20pm    
is shared hosting from solidseovps
Dave Kreskowiak 29-Feb-16 14:24pm    
Then you're going to have to ask your hosting company what the setting is that has to be changed and where you have to change it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900