使用请求的最大图像文件上载大小 [英] Max image file upload size using request

查看:80
本文介绍了使用请求的最大图像文件上载大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用chevereto创建了一个私有图像托管网站,我已经购买了它,我需要它直接用api上传图像。我有下面的代码,只适用于1 mb或更小的图像,1 mb不知道确切的大小。我得到(400)错误的请求,网站的error_log说:空上传源。我猜是streamWriter但我真的不知道为什么。我确实检查了Max上传大小,Max post size,Memory Limit和Max执行时间,它们的值是1024mb,1024mb,1024mb,300s。

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:我知道链接可以更好但我很懒。



我尝试了什么:



在没有太大成功的情况下搜索相同或类似的问题。尝试使用直接图像到base64,但这只是让它根本不起作用。这是使用的base64函数:

使用MemoryStream作为新的MemoryStream

Image.Save(MemoryStream,ImageFormat)

Dim Result As String = Convert.ToBase64String(MemoryStream.ToArray())

MemoryStream.Close()

返回结果

结束使用


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

推荐答案

您上传到的网站必须允许内容那么大。我假设一个ASP.NET网站。



在您网站项目的Web.config文件中,应该有一个< system .webServer> 配置项。如果没有,请将其添加到< configuration> 标记:

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>



上面的示例将请求内容限制设置为1GB。


The example above sets the request content limit at 1GB.


这篇关于使用请求的最大图像文件上载大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆