确保用户不要上传大小超过1MB的图像的最佳方法

时间:2021-12-23 01:50:12

I am using ASP.net with VB.NET. Is there some Validator that I can use that will check the size of the uploaded image? Else what must I do in code to make sure that the user do not upload images more than 1MB?

我在VB.NET中使用ASP.net。我可以使用一些验证器来检查上传图像的大小吗?否则我必须在代码中做些什么来确保用户不上传超过1MB的图像?

p.s C# code will also do, I can use a converter to VB.NET

p.s C#代码也会这样做,我可以用转换器来VB.NET

EDIT

For some reason when I change the maxRequestLength to 1024 (in my WEB.config) and I upload a image with size 1.25mb then I get the Microsoft Error page saying "Internet Explorer cannot display the webpage". And I do have a Try Catch block inside my Submit button. If I remove the maxRequestLength from my Web.config then it works fine.

出于某种原因,当我将maxRequestLength更改为1024(在我的WEB.config中)并上传大小为1.25mb的图像时,我得到Microsoft错误页面,说“Internet Explorer无法显示网页”。我的提交按钮里面有一个Try Catch块。如果我从我的Web.config中删除maxRequestLength然后它工作正常。

3 个解决方案

#1


This is ultimately handled in Web.config. Look for the httpRuntime section:

这最终在Web.config中处理。查找httpRuntime部分:

<httpRuntime 
 executionTimeout="110" 
 maxRequestLength="4096" 
/>

There are many other settings in httpRuntime but these are the two that are relevant. Here, the maxRequestLength is set to 4096, or 4KB (the number is in bytes). So, set this value accordingly. Also, you will want to set the executionTimeout accordingly as well so it gives a reasonable amount of time to upload whatever you max upload is.

httpRuntime中还有许多其他设置,但这两个设置是相关的。这里,maxRequestLength设置为4096或4KB(数字以字节为单位)。因此,请相应地设置此值。此外,您还需要相应地设置executionTimeout,因此它可以提供合理的时间来上传您上传的最大值。

#2


You can use the following code to determine the size [in KB] of the uploaded file and once you know the size you can easily decide if you want to proceed further with the file or reject the upload.

您可以使用以下代码确定上载文件的大小[以KB为单位],一旦知道大小,就可以轻松决定是否要继续使用该文件或拒绝上传。

Request.Files(0).ContentLength / 1024

#3


The cannot display web page error occurs because ASP.NET breaks the connection for oversized requests to mitigate DOS attacks based on oversized requests. To get around this, you'd have to do the upload in a iframe and then detect whether an error occurred or not. You could also use a flash, silverlight, java, or activex uploader component installed on the client to validate the file size client side, but that will require installation depending on your solution.

无法显示网页错误,因为ASP.NET断开了超大请求的连接,以减轻基于超大请求的DOS攻击。要解决这个问题,您必须在iframe中进行上传,然后检测是否发生了错误。您还可以使用客户端上安装的flash,silverlight,java或activex uploader组件来验证文件大小客户端,但这需要根据您的解决方案进行安装。

#1


This is ultimately handled in Web.config. Look for the httpRuntime section:

这最终在Web.config中处理。查找httpRuntime部分:

<httpRuntime 
 executionTimeout="110" 
 maxRequestLength="4096" 
/>

There are many other settings in httpRuntime but these are the two that are relevant. Here, the maxRequestLength is set to 4096, or 4KB (the number is in bytes). So, set this value accordingly. Also, you will want to set the executionTimeout accordingly as well so it gives a reasonable amount of time to upload whatever you max upload is.

httpRuntime中还有许多其他设置,但这两个设置是相关的。这里,maxRequestLength设置为4096或4KB(数字以字节为单位)。因此,请相应地设置此值。此外,您还需要相应地设置executionTimeout,因此它可以提供合理的时间来上传您上传的最大值。

#2


You can use the following code to determine the size [in KB] of the uploaded file and once you know the size you can easily decide if you want to proceed further with the file or reject the upload.

您可以使用以下代码确定上载文件的大小[以KB为单位],一旦知道大小,就可以轻松决定是否要继续使用该文件或拒绝上传。

Request.Files(0).ContentLength / 1024

#3


The cannot display web page error occurs because ASP.NET breaks the connection for oversized requests to mitigate DOS attacks based on oversized requests. To get around this, you'd have to do the upload in a iframe and then detect whether an error occurred or not. You could also use a flash, silverlight, java, or activex uploader component installed on the client to validate the file size client side, but that will require installation depending on your solution.

无法显示网页错误,因为ASP.NET断开了超大请求的连接,以减轻基于超大请求的DOS攻击。要解决这个问题,您必须在iframe中进行上传,然后检测是否发生了错误。您还可以使用客户端上安装的flash,silverlight,java或activex uploader组件来验证文件大小客户端,但这需要根据您的解决方案进行安装。