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
close icon

Post Upload Information

Hi, 
I am using an upload box in an MVC application and was wondering how to send back pass or fail information back to the view.  
I am using the upload box to load an Excel file into a dataset and eventually insert that data into a sql database.  All of that is working nicely when the data is correct in the Excel and If the data is not ok, it correctly shows a red X in the upload box to the user signifying that the upload failed.
I would also like to pass on more data to the user from the Controller(count of records inserted or failed, etc...) but I can't seem to update my view.  
I have tried to pass data to the viewbag but it looks like I need to use the return Content statement or else the upload box will show the red X even if it is successful.  
How can this be done?
Thanks

7 Replies

GG Gopi Govindasamy Syncfusion Team January 17, 2017 11:54 AM UTC

Hi  
 
We are sorry that we couldn’t understand the reported query clearly. Can you please provide more details on your requirement. It will help us to provide a solution.  
  
Please let us know if you need any other help.  
  
Regards,  
Gopi G. 



RD Richard Dublon January 17, 2017 01:46 PM UTC

Sorry for the confusion.
Basically I am trying to update my view with controller information after using the upload box.  Is a post or refresh happening or something else that is causing the viewbag not to be displayed?


RD Richard Dublon January 18, 2017 04:40 PM UTC

I have another question besides the one above.  How do I pass something back from within the return Content  statement that doesn't result in a red X when the import has completed.

I have the following:

                result = successCount + "_" + failedCount;

                return Content(result);

If I do the above, my upload box shows a red X as if the file failed even though it uploaded correctly.

Thanks


RD Richard Dublon November 6, 2017 09:26 PM UTC

All of this worked great...thank you.

I have another question.  Is there a way to pass data besides the file name to my controller?  For example, lets say I want to append some text to my file name in javascript in the file selected event as an identifier and I want to do something with that extra text in my controller in the same action result as my file upload...can this be done?




KR Keerthana Rajendran Syncfusion Team November 7, 2017 06:06 AM UTC

Hi Richard,   
  
Query: Is there a way to pass data besides the file name to my controller?  For example, lets say I want to append some text to my file name in javascript in the file selected event as an identifier and I want to do something with that extra text in my controller in the same action result as my file upload...can this be done?   
 
Response: Yes, you can pass additional data to controller through begin event of Uploadbox as shown below.   
     
@Html.EJ().Uploadbox("UploadDefault").SaveUrl("SaveDefault").RemoveUrl("RemoveDefault").ClientSideEvents(e=>e.FileSelect("onselect").Begin("uploadbegin"))   
    <script>   
        function onselect(args)   
        {   
            for (i = 0; i < args.files.length;i++)   
            {   
                args.files[i].name= args.files[i].name + "file" +i   
            }   
        }   
   
        function uploadbegin(args)   
        {   
              args.data= args.files.name    
        }   
    </script>   
 
Pass the data during begin event in script section and receive this string as additional parameter with Upload box id as shown below   
  public ActionResult SaveDefault(IEnumerable<HttpPostedFileBase> UploadDefault , string UploadDefault_data)   
         {   
             foreach (var file in UploadDefault)   
             {   
                 var fileName = Path.GetFileName(file.FileName);   
                 var destinationPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);   
                 file.SaveAs(destinationPath);   
             }   
            ViewData["data"] = UploadDefault_data;   
            return Content(UploadDefault_data);   
         }   
 
We have attached a sample for reference. Please download the sample from:   
Please refer the below KB for more details:   
   
Regards,   
Keerthana.   
 



MM Mohd Mohaimin Zakaria replied to Richard Dublon April 2, 2019 04:48 AM UTC

I have another question besides the one above.  How do I pass something back from within the return Content  statement that doesn't result in a red X when the import has completed.

I have the following:

                result = successCount + "_" + failedCount;

                return Content(result);

If I do the above, my upload box shows a red X as if the file failed even though it uploaded correctly.

Thanks

I'm also having a problem that the error shows "File failed to upload" even though the file is successfully uploaded.
Are there any issue or we cannot return anything back to the view ?


PO Prince Oliver Syncfusion Team April 2, 2019 10:07 AM UTC

Hello Mohd, 

Thank you for contacting us. 

No, we can return the content back to the view. We have ensured the reported scenario in our end by passing number of files selected using the begin event and returned this count from Controller through saveUrl method. This value can be retrieved in success event through “args.responseText” . Kindly refer to the following code. 
 
[View] 
@Html.EJ().Uploadbox("UploadDefault").SaveUrl("SaveDefault").ClientSideEvents(e =>e.Begin("uploadbegin").FileSelect("onSelect").Success("onSuccess")) 
        <script> 
            var length; 
            function onSelect(args) { 
                length = args.files.length; 
            } 
 
            function uploadbegin(args) { 
                args.data = length 
            } 
 
            function onSuccess(args) { 
                console.log(args.responseText); 
            } 
        </script> 
 
 
[Controller] 
public ActionResult SaveDefault(IEnumerable<HttpPostedFileBase> UploadDefault, string UploadDefault_data) 
        { 
            foreach (var file in UploadDefault) 
            { 
                var fileName = Path.GetFileName(file.FileName); 
                var destinationPath = Path.Combine(Server.MapPath("~/App_Data"), fileName); 
                file.SaveAs(destinationPath); 
                result = UploadDefault_data; 
            } 
            ViewData["data"] = UploadDefault_data; 
            return Content(result); 
        } 
 
 
We have attached the test sample and video demonstration for your reference, kindly refer to the following link 
 
Please check the above sample and if issue persists, please modify the above sample to replicate the issue. Else share us further details on the issues such as,  
 
  • Whether there are any console errors
  • Whether the success event is triggered
  • What error do you receive in the error event of UploadBox?
  • If possible, share us a video on the issue.
 
This will help us isolate the root cause and provide a prompt solution at the earliest. 

Regards, 
Prince 


Loader.
Live Chat Icon For mobile
Up arrow icon