**You can check this file size in server side using jquery webmethod**
As stated by hoodedperson70 here:
Sure thing, here's an example, but I cannot test...it should work
though:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#submit1").click(function () {
var file == $("#file1");
if (file.value = "" || file.value == null)
{
alert("You must provide a file.");
}
else
{
ajaxFileUpload();
}
});
});
function ajaxFileUpload()
{
//starting setting some animation when the ajax starts and completes
$("#loading").ajaxStart(function () {
$(this).show();
})
.ajaxComplete(function () {
$(this).hide();
});
/*
preparing ajax file upload
url: the url of script file handling the uploaded files
fileElementId: the file type of input element id
dataType: it supports json, xml
secureuri: use secure protocol
success: call back function when the ajax complete
error: callback function when the ajax failed
*/
$.ajaxFileUpload
(
{
url: 'THE WEB SERVICE URL',
secureuri: false,
fileElementId: 'file1',
dataType: 'json',
success: function (data, status)
{
if (data.success == "true")
{
$("#results").html("Valid file size, " + data.length);
}
else
{
$("#results").html("Invalid file size, " + data.length);
}
},
error: function (data, status, e)
{
alert(data + ", " + e);
}
}
)
return false;
}
</script>
</head>
<body>
<form id="form1" method="POST" action="" enctype="multipart/form-data">
<input type="file" id="file1" name="file1" />
<input type="button" id="submit1" name="submit1" value="Upload (size will be checked)" />
<img src="loading.gif" alt="Loading" id="loading" style="display:none;" />
<br /><br /><br />
<div id="results"></div>
</form>
</body>
</html>
And here's the web service code:
[WebMethod]
public string CheckFileSize(HttpContext context)
{
var file1 = context.Request.Files[0];
if (file1 != null && file1.ContentLength > 0)
{
if (file1.ContentLength > 1024) // This is the IMPORTANT comparison
{
return "{\"success\":\"true\",\"length\":\"" + file1.ContentLength + "\"}";
}
else
{
return "{\"success\":\"false\",\"length\":\"" + file1.ContentLength + "\"}";
}
}
}
You will need to follow this link:
http://www.phpletter.com/download_project_version.php?version_id=34 to
download the .js file and the "loading.gif" file. For my example, they
are in the same directory, but you may not have that, so you'll need
to change their paths if necessary.
I hadn't realized they use PHP as the processing, but this should
still work just the same. The javascript should be able to submit the
file to any type of technology, so it depends on the web service
logic. It's very possible that the use of HttpContext isn't right for
this scenario, but you can test this out.
More than way's for client side validation
else , simply try this way
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
//Display a warning
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file here
}