Is MVC better than webform? Not to mention the none. I personally think the biggest benefit of MVC is code concentration. High reuse rate. In particular, it is nice to use a strong visual view. Integration. Highly integrated.
Our project is still stuck in a combination of mvc2.0 + ado.net + webform (individual pages) + jquery. Share the MVC-based Brushless new upload control (no progress bar) that you have developed ).
This is a function call method: BaseUploadFile (1024*100, new List <string> () {"jpg", "jpeg", "png", "gif", "JPG ", "JPEG", "PNG", "GIF"}); parameter 1 indicates the size; parameter 2 indicates the suffix name.
Protected ActionResult BaseUploadFile (int Maxlength, List <string> postfix)
{
Try
{
HttpPostedFileBase httpfile = Request. Files ["FileMy"];
String [] str = httpfile. FileName. Split ('.');
String extion = str [str. Length-1];
Bool isExtion = false;
Foreach (string pos in postfix)
{
If (pos = extion)
{
IsExtion = true; break;
}
}
If (string. IsNullOrEmpty (extion) | (isExtion = false ))
{
Return Content ("1 ");
}
String saveUrl = "/upload/UploadMVC/" + Guid. NewGuid (). ToString () + "." + extion + "";
If (httpfile. ContentLength> Maxlength)
{
Return Content ("2 ");
}
Httpfile. SaveAs (Server. MapPath (saveUrl ));
Return Content (saveUrl );
}
Catch (Exception ex)
{
Return Content ("3 ");
}
}
The front-end code here uses a jquery plug-in Form. js du Niang.
This is the result.
View Code
<Script type = "text/javascript">
Function showResponse (responseText, statusText, xhr, $ form ){
If (responseText = "1 "){
Alert ("the upload format is incorrect! "); Return false;
}
If (responseText = "2 "){
Alert ("the size of the uploaded file exceeds the upload size limit! "); Return false;
}
If (responseText = "3 "){
Alert ("Upload error"); return false;
}
$ ("# NNimgUrl"). attr ("src", responseText );
}
Function sumbitFile (){
Var options = {
Success: showResponse
}
$ ('# MyForm1'). ajaxSubmit (options );
}
</Script>
<Form action = "/Form/uploadFile" method = "post" enctype = "multipart/form-data" id = "myForm1">
& nbsp; <input id = "FileMy" name = "FileMy" type = "file" style = "background-color: # D9D9D9; color: #009999 "/> <input id =" Button1 "onclick =" sumbitFile () "type =" button "value =" Upload "style =" background-color: #996633; color: # FFFFFF "/>
</Form>
<Div id = "dics"> </div>
From qingniu Ke