Re: MVC Ajax Uploader

  •  02-28-2013, 12:55 PM

    Re: MVC Ajax Uploader

    Hi Avi,

     

    1. Is there a way to pass data from javascript to UploadHandler.ashx before a the file is uploaded, or after the file are selected?
     

    I suggest you handle the upload file in the controller directly. So you can pass the value to the controller directly.

     

    refer to the example below, you can save/move or do other control of the upload file directly.

     

    public ActionResult Start_uploading_manually(string myuploader)
                {
                      using (CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(System.Web.HttpContext.Current))
                      {
                            uploader.UploadUrl = Response.ApplyAppPathModifier("~/UploadHandler.ashx");
                            //the data of the uploader will render as <input type='hidden' name='myuploader'> 
                            uploader.Name = "myuploader";
                            uploader.AllowedFileExtensions = "*.jpg,*.gif,*.png,*.bmp,*.zip,*.rar";
                            uploader.MaxSizeKB = 1024;
                            uploader.ManualStartUpload = true;
                            uploader.InsertText = "Browse Files (Max 1M)";
                                         //prepair html code for the view
                            ViewData["uploaderhtml"] = uploader.Render();
                   
                            //if it's HTTP POST:
                            if (!string.IsNullOrEmpty(myuploader))
                            {
                                  //for single file , the value is guid string
                                  Guid fileguid = new Guid(myuploader);
                                  CuteWebUI.MvcUploadFile file = uploader.GetUploadedFile(fileguid);
                                  if (file != null)
                                  {
                                        //you should validate it here:
                                        //now the file is in temporary directory, you need move it to target location
                                        //file.MoveTo("~/myfolder/" + file.FileName);
                                        //set the output message
                                        ViewData["UploadedMessage"] = "The file " + file.FileName + " has been processed.";
                                  }
                            }
                      }
                      return View();
                }

    2. I tried to render the MVC Uploader in partial view that exist on my main view 4 times and got an error :
     
    Microsoft JScript runtime error: global event handler error #CuteWebUI_AjaxUploader_OnQueueUI : Unable to get value of the property 'FileName': object is null or undefined
     
     When I render the
       @{Html.BeginForm();}
                        @Html.Raw(ViewBag.uploaderhtml)
    @{Html.EndForm();} 
     
    on the main view it's work fine, but on the partial view I get the error above.
     

    can you create an example which can reproduce this issue and send it to Kenneth@CuteSoft.net? So I can check the code directly. 


    3. The queue list always have ALL the file I uploaded and not just the last files I upload.
        even if I try to clean the rows with Jquery Script, the queue is cleaned but when I start a new upload, again it's show ALL the files
        I uploaded, is there a way to avoid that?
     
    4. If in the UploadHandler.ashx OnFileUploaded(MvcUploadFile file) event I try to move the uploaded file (from temp directory) into another directory (or copy and delete it)  I get an error "File does not exist : {file guid} "
    I want to move it in order to clean the temp directory and put it in the right place
     

    please refer to the example in question 1, you can use method "MoveTo" to save the upload file, and this method will clean the temp files too.


    5.  is there a way to avoid using UploadHandler.ashx and post the file into the mvc controller?

     

    Please refer to the example in question 1, you can handle the upload file directly in the controller.

     

    Regards,

     

    Ken
     

View Complete Thread