We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

force upload file

Hi,

I've some uploadbox out of form, and I need upload the files in synchronous.

so, how can I force upload files when I need?

I have not seen any method for force upload the files.

Thanks

6 Replies

KV Karthikeyan Viswanathan Syncfusion Team February 14, 2017 08:53 AM UTC

Hi Manolo,      
     
Thanks for contacting Syncfusion Support.      
  
Yes, you can achieve this scenario using Ajax post without form element. Please find the below code snippet:      
     
<code>      
@Html.EJ().Uploadbox("UploadDefault").SaveUrl("SaveDefault").RemoveUrl("RemoveDefault").AsyncUpload(false)   
   
                       
 @Html.EJ().Button("btnclick").Type(ButtonType.Button).Text("Click")   
   
</code>      
<code>   
<script type="text/javascript">   
        $(function () {   
            $("#btnclick").click(function (args) {   
                var formdata = new FormData();   
                var _files = $("[name='UploadDefault']:not(.e-uploadinput)")[0].files.length;   
                for (var x = 0; x < _files; x++) {   
                    formdata.append("fileInput[" + x + "]"$("[name='UploadDefault']:not(.e-uploadinput)")[0].files[x]);   
                }   
                $.ajax   
                           (   
                                  {   
                                      url: '@Url.Action("SaveSynchronous""FileUpload")',  //'/Tab/QuickListTabContent',   
                                      type: 'POST',   
                                      data: formdata,   
                                      processData: false,   
                                      contentType: false,   
                                      cache: false,   
                                  }).done(function (result) {   
                                      $("[name='UploadDefault']:not(.e-uploadinput)").each(function () {   
                                              $(this).remove();   
                                      });   
                                      var inst = $("#UploadDefault").ejUploadbox("instance");   
                                      inst.updialog.data('ejDialog').close();   
                                  }   
                           );   
            });   
        });   
    </script>   
</code>   
<code>   
Controller   
[HttpPost]   
        public ActionResult SaveSynchronous(IEnumerable<HttpPostedFileBasefileInput)   
        {   
            if (fileInput != null)   
            {   
                foreach (var file in fileInput)   
                {   
                    var fileName = Path.GetFileName(file.FileName);   
                    var destinationPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);   
                    file.SaveAs(destinationPath);   
                }   
                TempData["status"] = "Successfully Uploaded";   
                return RedirectToAction("FileUploadFeatures");   
            }   
            else   
            {   
                TempData["status"] = "Select a file to upload";   
                return RedirectToAction("FileUploadFeatures");   
            }   
   
        }   
   
   
</code>   
  
     
     
Regards,      
Karthikeyan V.     



MA Manolo February 15, 2017 08:42 AM UTC

Thanks a lot!


MA Manolo February 15, 2017 12:27 PM UTC

Ups... other question. In ajax petition. how can I add more parameters?

Some similar at this

 $.ajax({
                url: '@Url.Action("SaveSynchronous", "NuevasPoblaciones")', 
                type: 'POST',
                data: { fileInput: JSON.stringify(formdata), idConflicto: "1" },
                processData: false,               
                contentType: false,
                cache: false,
            }).done(function (result) {
                $("[name='ubFicheroCompetencia']:not(.e-uploadinput)").each(function () {
                    $(this).remove();
                });
                var inst = $("#ubFicheroCompetencia").ejUploadbox("instance");
                inst.updialog.data('ejDialog').close();
            }
            );

public ActionResult SaveSynchronous(IEnumerable<HttpPostedFileBase> fileInput, string idConflicto)
{
...}

When I add a new parameter, in controller, all parameters has null values


KV Karthikeyan Viswanathan Syncfusion Team February 16, 2017 05:48 AM UTC

Hi Manolo,         
        
Thanks for your update.     
  
Yes, you can easily add more parameters in form data using the highlighted code below. Please find the code snippet:   
  
<code>   
  
$(function () {   
            $("#btnclick").click(function (args) {   
                var formdata = new FormData();   
                var _files = $("[name='UploadDefault']:not(.e-uploadinput)")[0].files.length;   
                for (var x = 0; x < _files; x++) {   
                    formdata.append("fileInput[" + x +"]", $("[name='UploadDefault']:not(.e-uploadinput)")[0].files[x]);   
                }   
                formdata.append("idConflicto", "1");   
                $.ajax   
                           (   
                                  {   
                                      url:'@Url.Action("SaveSynchronous", "FileUpload")',   
                                      type: 'POST',   
                                      data: formdata,   
                                      processData: false,   
                                      contentType: false,   
                                      cache: false,   
                                  }).done(function(result) {   
                                      $("[name='UploadDefault']:not(.e-uploadinput)").each(function() {   
                                             $(this).remove();   
                                      });   
                                      var inst = $("#UploadDefault").ejUploadbox("instance");   
                                     inst.updialog.data('ejDialog').close();   
                                  }   
                           );   
            });   
        });   
    </script>   
   
   
// Controller   
   
   
public ActionResultSaveSynchronous(IEnumerable<HttpPostedFileBase> fileInput,String idConflicto)   
        {   
               
        }   
</code>   
 
  



  
 



MA Manolo February 17, 2017 08:25 AM UTC

Thanks again!


KV Karthikeyan Viswanathan Syncfusion Team February 20, 2017 04:17 AM UTC

Hi Manolo,  
  
Thanks for the update.   
  
We are glad the suggestion helped you to achieve your requirement.   
  
Please let us know if you need further assistance.   
  
Regards,    
Karthikeyan V.

Loader.
Up arrow icon