0

On my asp.net mvc 4 site I have a feature where a user can upload a photo, via standard file uploader. The photo gets saved in to a file table within sql server.

I have run in to an issue recently where users are uploading very large photos which in return means bandwidth being eaten up when image is being rendered.

What is the best way to handle this? Can I restrict the size of file being uploaded? Or is there a way of reducing the number of bytes being uploaded while maintaining quality?

amateur
  • 43,371
  • 65
  • 192
  • 320

1 Answers1

0

Refer to this post for the maximumRequestLength config setting and way to provide a more friendly error

This question and answer may also be helpful

You can also check the size of the file in javascript before uploading so that it doesn't even get sent to the server if it is too big (the code below check for anything bigger than 15MB):

if( Math.floor( file.size / 1024 /1024 ) >= 15 )
{ 
    alert( 'File size is greater than maximum allowed. Please make sure that the file is smaller than 15 MegaBytes.'  );
     return false;
}

Alternatively, on the server side you can use WebImage.Resize() to resize once the file has been uploaded. It won't help with the bandwidth during upload, but it will make subsequent downloads a lot faster. Making an image smaller will cause some loss in quality, but generally it does a good job, just make sure that you choose the option to maintain the aspect ratio to prevent distortion.

As for reducing the bytes before uploading there isn't any way I know of to do this in the browser. You could provide a separate client-side application that will resize the files for them before the upload using the WebImage.Resize method in you app.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94