如何在c#.net Windows中限制上传的大小 [英] how to restrict the size of upload in c#.net windows
本文介绍了如何在c#.net Windows中限制上传的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要以byte []的形式将图像上传到数据库.我需要限制上传的大小,例如5mb,这是如何在c#窗口中以编程方式实现的.
问候,
sajith
iam uploading images to the database as byte[].i need to restrict the size of upload say 5mb how it is possible in c# windows programically.
Regards,
sajith
推荐答案
您需要在web.config文件中应用设置.它会影响整个应用程序,但是...我不认为您可以为每页设置它.
You need to apply the setting in your web.config file. It affects the entire application, though... I don''t think you can set it per page.
<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>
xxx为KB.默认值为4096(= 4 MB).
xxx is KB. The default is 4096 (= 4 MB).
使用5242880
检查图像字节数组的长度.
Check image byte array length with 5242880
if(ImageArray.Length > 5242880)
{
//Display error message.
}
else
{
//Upload image byte array.
}
public bool Limit(string path)
{ const int limit = 5;
float size = ToByteArray(path).Length / 1024f / 1024f;
if (size > limit)
{ SendMessage("Uploading Limit Exceeds");
return false; }
return true; }
public byte[] ToByteArray(string path)
{ byte[] buffer = File.ReadAllBytes(path); return buffer; }
这篇关于如何在c#.net Windows中限制上传的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文