Re: Can ajax upload file to a controller instead of uploadHanler.ashx?

  •  11-20-2012, 9:34 AM

    Re: Can ajax upload file to a controller instead of uploadHanler.ashx?

    Hi,

     

    Here is the demo ,

     

    #1 ,modify the uploader.UploadUrl to the action url :

     

    1. @{ 
    2.     CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(HttpContext.Current); 
    3.  
    4.     uploader.Name = "MyUploader"
    5.     uploader.AllowedFileExtensions = "*.png,*.jpg,*.rar"
    6.     uploader.MaxSizeKB = 200; 
    7.     uploader.UploadUrl = Url.Action("UploadHandler"); 
    8.      
    9.     @Html.Raw(uploader.Render()); 
     

    #2 , in the action UpoadHandler, do this way :

     

    1. [HttpPost] 
    2. public ActionResult UploadHandler() 
    3.     CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(System.Web.HttpContext.Current); 
    4.     uploader.AllowedFileExtensions = "*.png,*.jpg,*.rar"
    5.     uploader.MaxSizeKB = 200; 
    6.     uploader.PreProcessRequest(); 
    7.  
    8.     if (uploader.IsValidationRequest) 
    9.     { 
    10.         CuteWebUI.MvcUploadFile file = uploader.GetValidatingFile(); 
    11.         if (file.FileSize < 100000) 
    12.         { 
    13.             uploader.WriteValidationError("File is too small"); 
    14.         } 
    15.         else 
    16.         { 
    17.             uploader.WriteValidationOK("OK"); 
    18.         } 
    19.         Response.End(); 
    20.     } 
    21.  
    22.     return View(); 

    This way is just help you move the code from UploadHandler.ashx to the mvc controller .

     

    You shall handle the uploader postback like our samples do .

     

    Regards,

    Terry

     

     

     

View Complete Thread