My FileUpload control is in a Jquery Dialog with Few other fields, This Jquery Dialog is a child view. I want to validate File size on Server and display Error message if it's above certain size. I am using following Approach
1. Jquery Dialod can be openned from Parent View. THere are few ext Boxes plus <input type=file> on my Child view( Jquery Dialog)
2. Since i can't check file size on client hence i am forced to post it to my Action and validate is size.
3. Juqery dialog is being posted to some XYZ Action. Here i amk checking size and if it's not valid, i am doing following steps.
i) Putting Posted Model to TempData["model"]=model;
ii) Putting Error Message in TempData["Error"]="File Size has exceed it's limit";
iii) Redirecting it to the Action of Parent View e.g. Lets call it Pareant View Action RedirctToAction("ParentViewAction");
iv) on PArent View i am checking if TempData["Error"]!=Null then i am Rendering ChildView As Partial View and Passing it Model from TempData["model"] as we did create it in step ( i )
In this way i managed to open Parent and Child views at once and pass selected (entered) data in child view as well.
Problem: I have two questions
1 ) -> Is there any better approach of doing this. I have described the who scenario
2 )-> in Internet Explorer my Screen Flickers for a second o so when both views gets loaded. For a second i can see Parent view and child view being inserted at the bottom of the page and then finally Jquery Dialog. This does not happen in FireFox and Views
are loading very smoothly. What can i do to make it smooth?
I am openning my child View in a Dialog and Dont know if it's possible without JQuery Dialog? Any possibility of checking file Size on Client? I am using IE 8 and 9 and think it's not supporting HTML 5 file system API as yet
DataAnotations way: define a validate attribute like this:
public class FileAttribute : ValidationAttribute
{
public int MaxContentLength = int.MaxValue;
public override bool IsValid(object value)
{
var file = value as HttpPostedFileBase;
//this should be handled by [Required]
if (file == null)
return true;
if (file.ContentLength > MaxContentLength)
{
ErrorMessage = "File is too large, maximum allowed is: {0} KB".FormatWith(MaxContentLength / 1024);
return false;
}
return true;
}
}
saadat Ali
Member
60 Points
57 Posts
File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 09:32 AM|LINK
My FileUpload control is in a Jquery Dialog with Few other fields, This Jquery Dialog is a child view. I want to validate File size on Server and display Error message if it's above certain size. I am using following Approach
1. Jquery Dialod can be openned from Parent View. THere are few ext Boxes plus <input type=file> on my Child view( Jquery Dialog)
2. Since i can't check file size on client hence i am forced to post it to my Action and validate is size.
3. Juqery dialog is being posted to some XYZ Action. Here i amk checking size and if it's not valid, i am doing following steps.
i) Putting Posted Model to TempData["model"]=model;
ii) Putting Error Message in TempData["Error"]="File Size has exceed it's limit";
iii) Redirecting it to the Action of Parent View e.g. Lets call it Pareant View Action RedirctToAction("ParentViewAction");
iv) on PArent View i am checking if TempData["Error"]!=Null then i am Rendering ChildView As Partial View and Passing it Model from TempData["model"] as we did create it in step ( i )
In this way i managed to open Parent and Child views at once and pass selected (entered) data in child view as well.
Problem: I have two questions
1 ) -> Is there any better approach of doing this. I have described the who scenario
2 )-> in Internet Explorer my Screen Flickers for a second o so when both views gets loaded. For a second i can see Parent view and child view being inserted at the bottom of the page and then finally Jquery Dialog. This does not happen in FireFox and Views are loading very smoothly. What can i do to make it smooth?
dinord2006
Member
141 Points
101 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 01:09 PM|LINK
Hi saadat !!!
You can do this without using JQuery...do you want absolutely use it?
saadat Ali
Member
60 Points
57 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 01:27 PM|LINK
I am openning my child View in a Dialog and Dont know if it's possible without JQuery Dialog? Any possibility of checking file Size on Client? I am using IE 8 and 9 and think it's not supporting HTML 5 file system API as yet
dinord2006
Member
141 Points
101 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 01:42 PM|LINK
Have a look in this link :
http://stackoverflow.com/questions/4681727/file-upload-controls-using-razor-in-asp-net-mvc3
hope it will helpfull
dinord2006
Member
141 Points
101 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 01:44 PM|LINK
And this :
http://stackoverflow.com/questions/4884920/mvc3-valums-ajax-file-upload
dinord2006
Member
141 Points
101 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 01:46 PM|LINK
Keep me inform
saadat Ali
Member
60 Points
57 Posts
Re: File size check on Server Side and redirectig to same View with Error message
Mar 01, 2012 03:11 PM|LINK
None of these checks file size on client side
Young Yang -...
All-Star
21343 Points
1818 Posts
Microsoft
Re: File size check on Server Side and redirectig to same View with Error message
Mar 07, 2012 07:05 AM|LINK
Hi
DataAnotations way: define a validate attribute like this:
public class FileAttribute : ValidationAttribute { public int MaxContentLength = int.MaxValue; public override bool IsValid(object value) { var file = value as HttpPostedFileBase; //this should be handled by [Required] if (file == null) return true; if (file.ContentLength > MaxContentLength) { ErrorMessage = "File is too large, maximum allowed is: {0} KB".FormatWith(MaxContentLength / 1024); return false; } return true; } }Then in the model:
[File( MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")] public HttpPostedFileBase myfile { get; set; }And there are many upload plugin can help you check the file size on the client.(Uploadify, Plupload ...), and also you can check this blog post:http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx
Hope this helpful
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store