Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

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
Posted

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.

XML
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="xxx" />
  </system.web>
</configuration>



xxx is KB. The default is 4096 (= 4 MB).
 
Share this answer
 
Comments
sajithnet 28-Mar-12 2:06am    
thanks u ganesan,
i got the answer.

public bool Limit(string path) {
const int limit = 5;
float size = ToByteArray(path).Length / 1024f / 1024f;//first convertto kb then to mb
if (size > limit) {
SendMessage("Uploading Limit Exceeds");
return false;
}
return true;
}

public byte[] ToByteArray(string path) {
byte[] buffer = File.ReadAllBytes(path);
return buffer;
}
Check image byte array length with 5242880

C#
if(ImageArray.Length > 5242880)
{
//Display error message.
}
else
{
//Upload image byte array.
}
 
Share this answer
 
Comments
sajithnet 28-Mar-12 2:06am    
thank you,mate
i got the answer. public bool Limit(string path) { const int limit = 5; float size = ToByteArray(path).Length / 1024f / 1024f;//first convertto kb then to mb if (size > limit) { SendMessage("Uploading Limit Exceeds"); return false; } return true; } public byte[] ToByteArray(string path) { byte[] buffer = File.ReadAllBytes(path); return buffer; }
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#

 
Share this answer
 

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