1

CONTRACT TABLE

[MaxLength(2000000)]
[Required, FileExtensions(Extensions = ".PDF"]
public byte[] Document { get; set; }

I want to limit the user to uploading a PDF file no greater than 2 MB in size. So, I used the MaxLength and FileExtension attributes. Despite adding MaxLength(2000000) the column in my Contract table has data type of Varbinary(Max). This implies a maximum size of 2^31 bytes. From what I understand SQL Server will allow me to specify a size between 1 and 8,000 bytes. After that, I have to settle for Varbinary(Max). Is this correct or does someone have a way that I can limit the File Size to 2MB? I would like this implemented on the SQL server side AND through my controller [MVC project]. The reason I want both protections is I want to prevent someone with direct access to the database from uploading a file that is too large. I also want to prevent a user of my website from uploading files that are too large.

  • 1
    Possible duplicate of [Can I set 2 MB for maximum size of varbinary?](https://stackoverflow.com/questions/34741079/can-i-set-2-mb-for-maximum-size-of-varbinary) – John Wu Nov 14 '18 at 05:29
  • Thanks. This is helpful. I can now update the column in the table from accepting files larger than 2 MB. It sounds like there is no way then to set this limit using attributes? So, I will have to write code that checks the size of the file a user uploads through my website using client side Javascript? – Deepak Sreedharan Nov 14 '18 at 05:49
  • 1
    In commercial web sites it is common to have redundant validation. As a convenience to your users it is probably best to double check the length in the client itself, which will save a user from having to wait for the file to post before seeing the error telling him he just wasted his time. You might also want another check in the web server to prevent hackers from attempting to upload very large files to cause a DoS. – John Wu Nov 14 '18 at 06:38

0 Answers0