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

how can I read a xlsx from a url

@model string


@Html.EJS().Spreadsheet("spreadsheet").Created("onCreated").Render()

<script>


     var fileName = "@Model";

    var documenteditor;

    function onCreated(args) {

        var container = document.getElementById("spreadsheet").ej2_instances[0];
        documenteditor = container.documentEditor;
        documenteditor.resize();

        $.ajax({
            url: "@Url.Action("OpenWord", "Download", new { area = "OperacionesGenerales" }, Request.Url.Scheme)",
            data: { fileName },
            beforeSend: function () {
                //Showloading();
            },
            success: function (doc) {
                //$("#descarga").html(doc);
                console.log(doc)
                documenteditor.open(doc);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
                alert("error");
            },
            complete: function () {

            }
        })
    }


</script>

@Html.EJS().ScriptManager()


I wold like to read a xlsx form a url but it doesnt work and I do not have the server function If you could give a sample it would be greatfull thanks for the attention


11 Replies

SD Saranya Dhayalan Syncfusion Team January 7, 2020 03:00 AM

Hi Ricardo, 
 
Good day to you. 
 
We would like to suggest you use XMLHttpRequest to get the file in the server and you can load it to the Spreadsheet control using open method. Please refer the below code snippets. 
 
Index.cshtml 
@Html.EJS().Spreadsheet("spreadsheet").Created("onCreated").OpenUrl("Home/Open").SaveUrl("Home/Save").Render() 
<script> 
    function onCreated() { 
        var spreadsheetObj = ej.base.getComponent(document.getElementById('spreadsheet'), 'spreadsheet'); 
        var request = new XMLHttpRequest(); 
        request.responseType = "blob"; 
        request.onload = () => { 
            var file = new File([request.response], "Sample1.xlsx"); 
            spreadsheetObj.open({ file: file }); 
        } 
        request.open("GET", "/Files/Sample1.xlsx"); // To Get the file from the URL 
        request.send(); 
    } 
</script> 
 
 
For your convenience, we have prepared a sample. Please find the below sample link 
Sample link:  
 
 
Could you please check the above sample link and get back to us, if you need any further assistance on this? 
 
Regards, 
Saranya D 



RI Ricardo January 7, 2020 08:17 AM

thanks but now it shows me this issue

I mean it works with couchDB database

Attachment: document_excel_a35bbf9.zip


SD Saranya Dhayalan Syncfusion Team January 8, 2020 02:41 AM

Hi Ricardo, 
  
Good day to you. 
  
We have checked your issue. We are not able to reproduce it in our end. Could you please confirm whether you are using the below code snippets for open method in the controller? The reported issue may occur when improper type returned by the Open method. 
  
public ActionResult Open(OpenRequest openRequest) 
  { 
            return Content(Workbook.Open(openRequest)); 
   } 
  
Please check your issue with the above code snippets, if the issue persists,  
 
1.       Please share your controller code snippets  
2.       Please share the dummy excel file for reproducing the issue. 
 
  
Could you please provide the above requested information, based on that we will check and provide you a better solution quickly. 
 
Regards, 
Saranya D 



RI Ricardo January 9, 2020 11:26 AM

ok it works with local files but I need It reads a url document but it shows me a message in the console and do not show the info from the document

Attachment: document_excel__1_b85faa93.zip


SD Saranya Dhayalan Syncfusion Team January 10, 2020 03:54 AM

Hi Ricardo, 
  
Good day to you. 
  
We have checked your issue and the request you are trying to get the file and it is prevented by your server, you need to enable CORS in your server to resolve your issue. We suggest you add the below code snippets in web.config file to resolve your issue. Please check the below link for further details. 
  
Web.config 
<system.webServer> 
<httpProtocol> 
<customHeaders> 
<add name="Access-Control-Allow-Origin" value="*" /> 
</customHeaders> 
</httpProtocol> 
</system.webServer> 
  
Please find the below link 
  
  
Please let me know if you have any questions. 
 
Regards, 
Saranya D 



RI Ricardo January 10, 2020 02:30 PM

I tried the solution, even with a nuget package which is specialized in COR access and it did not work 

Don´t you have any example in mvc 5 with COR access?


SD Saranya Dhayalan Syncfusion Team January 13, 2020 12:43 AM

Hi Ricardo, 
  
Good day to you. 
  
We have prepared a MVC5 sample with enabled CORS in web.config and you can access “Sample1.xlsx” file placed in the Files folder in another domain, please check with below attached sample whether it resolve your issue, if still persist, please replicate your issue in the sample, So that we can sort out and provide you the better solution quickly. 
  
  
Regards, 
Saranya D 



RI Ricardo January 13, 2020 08:13 AM

thanks a lot and sorry if I am bothering you but now it shows me an exception in the image I am sending to you

Attachment: document_excel_83d0ac34.zip


SD Saranya Dhayalan Syncfusion Team January 14, 2020 04:25 AM

Hi Ricardo,  
 
We have checked your reported issue, we suspect this issue will occur because the Syncfusion. Xlsio dll doesn’t refer. So could you please add this dll(same version) to resolve your problem. 
 
Please check the below link: 
 
 
Could you please check the above sample and get back to us, if you need further assistance on this? 
Regards, 
Saranya D 



RI Ricardo January 14, 2020 12:43 PM

thanks again but know it shows me that the request is dangerous for the client 

Attachment: document_excel_5d862795.zip


MV Madhan Venkateshan Syncfusion Team January 17, 2020 02:51 AM

Hi Ricardo, 
 
Good day to you. 
 
We suspect the issue may arise when the url contains potential dangerous characters (< > * % & : \ ?) which may be used in XSS attacks. We suggest you to add the requestValidationMode or add the invalid characters in the requestPathInvalidCharacters attribute in web.config file to resolve your issue and please check the below stackoverflow link for futher details. 
 
Web.config 
  <system.web> 
<httpRuntime requestValidationMode="2.0" requestPathInvalidCharacters="put your invalid character here" /> 
  </system.web> 
 
 
 
 
Please check the above link, if the issue still persist, please share the issue reproducible sample. So that we can sort out and provide you the better solution quickly.  
 
Regards, 
Madhan V 


Loader.
Live Chat Icon For mobile
Up arrow icon