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

Last post 11-21-2012, 1:03 AM by ThangLe. 5 replies.
Sort Posts: Previous Next
  •  11-14-2012, 11:21 AM 75259

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

    Hi.. my current needs is to get a dirrect file from a mvc form, but it's seem that uploader alsway upload to its default handler. It is possible to upload a file to a controller ?

     

    Please reply soon.. Thanks

    Filed under:
  •  11-18-2012, 11:15 PM 75282 in reply to 75259

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

    Yes,

     

    We will provide a sample in 24 hours.

     

    Regards,

    Terry

  •  11-20-2012, 9:34 AM 75290 in reply to 75259

    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

     

     

     

  •  11-20-2012, 10:31 PM 75299 in reply to 75290

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

    Hi. Terry.

    It works, thank you for your quick support. But I like to do little further. In the controller I got an action.

     

    [HttpPost]

    public ActionResult UploadHandler(string description)

    {

          CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(System.Web.HttpContext.Current);

          uploader.AllowedFileExtensions = "*.png,*.jpg,*.rar";

          uploader.TempDirectory = "~/App_Data";

                uploader.MaxSizeKB = 200;

          uploader.PreProcessRequest();

    if (uploader.IsValidationRequest)

    {

          CuteWebUI.MvcUploadFile file = uploader.GetValidatingFile();

          if (file.FileSize < 100000)

          {

          uploader.WriteValidationError("File is too small");

          }

    else

    {

          uploader.WriteValidationOK("OK");

          }

          Response.End();

          }

          return View();

    }

    and I view I write

     

    @{Html.BeginForm();}

    @Html.Raw(ViewBag.uploaderhtml)

    <input type="text" name="description" value=" " />

    <br />

    <br />

    <button id="SubmitButton" onclick="return submitbutton_click()">Submit</button>

    @{Html.EndForm();}

    <script type="text/javascript">

    function submitbutton_click() {

    var submitbutton = document.getElementById('SubmitButton');

    var uploadobj = document.getElementById('myuploader');

    if (!window.filesuploaded) {

    if (uploadobj.getqueuecount() > 0) {

    uploadobj.startupload();

    }

    else {

    var uploadedcount = parseInt(submitbutton.getAttribute("itemcount")) || 0;

    if (uploadedcount > 0) {

    return true;

    }

    alert("Please browse files for upload");

    }

    return false;

    }

    window.filesuploaded = false;

    return true;

    }

    function CuteWebUI_AjaxUploader_OnPostback() {

    window.filesuploaded = true;

    var submitbutton = document.getElementById('SubmitButton');

    submitbutton.click();

    return false;

    }

    </script>

     

    My point here is to get a fille and a description for it in the handler.  I had license about a year ago with username  ktran@datalinkage.com

    But just got this problem recently.

     

  •  11-20-2012, 11:06 PM 75300 in reply to 75299

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

    Hi,

     

    Here is the client side API to set the data

     

    1. <script> 
    2.     var inp = document.getElementById("textboxid"); 
    3.     var currtask; 
    4.  
    5.     inp.onchange = function () { 
    6.         if(currtask) 
    7.             currtask.SetClientData(inp.value); 
    8.     } 
    9.     function CuteWebUI_AjaxUploader_OnTaskStart(task) { 
    10.         currtask = task; 
    11.         currtask.SetClientData(inp.value); 
    12.     } 
    13. </script> 
     

     and here is how server side use it

     

    1. if (uploader.IsValidationRequest) 
    2.     string clientdata = Request.Form["_AjaxUploaderClientData_"]; 
    3.     uploader.WriteValidationError("Test : client data : "+clientdata); 
    4.     Response.End(); 
     

     Regards,

    Terry

     

     

  •  11-21-2012, 1:03 AM 75301 in reply to 75300

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

    Hi,

    I'm also try to get the server message in

    "uploader.WriteValidationError("test : file processed")"

     

    I did try some ajax like CuteWebUI_AjaxUploader_OnStop or CuteWebUI_AjaxUploader_OnPostback to

    get viewbag message from server, but it doesn't work. Can you provide me the full ajax api document, cause

    I cannot find the CuteWebUI_AjaxUploader_OnTaskStart that you used in your example in

    http://ajaxuploader.com/document/index.htm#page=JavaScript-API.htm 

     

    Is it has the event that fires when upload complete????

    thanks  

     

View as RSS news feed in XML