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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

how can I read a docx from a url

Thread ID:

Created:

Updated:

Platform:

Replies:

150374 Jan 4,2020 12:25 AM UTC Jan 9,2020 07:12 AM UTC ASP.NET MVC - EJ 2 7
loading
Tags: DocumentEditor
Ricardo
Asked On January 4, 2020 12:25 AM UTC

[HttpGet]
        public HttpResponseMessage OpenWord(string fileName)
        {
            //string path = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/");
            //path += fileName;

            var path = Constantes.Coucher_no_Auth + fileName;

            var webRequest = WebRequest.Create(path);

            Stream stream;

            using (var response = webRequest.GetResponse())
            using (var content = response.GetResponseStream())
            using (var reader = new StreamReader(content))
            {
                // var strContent = reader.ReadToEnd();
                stream = reader.BaseStream;
            }

            //if (!System.IO.File.Exists(path))
            //    return null;
            //Stream stream = System.IO.File.OpenRead(path);

            //WebClient wc = new WebClient();
            //Stream stream = wc.OpenRead(path);

            //using (StreamReader sr = new StreamReader(data))
            //{
            //    str = sr.ReadToEnd();
            //    data.Close();
            //}

            WordDocument document = WordDocument.Load(stream, FormatType.Docx);
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
            // Releases unmanaged and optionally managed resources.
            document.Dispose();
            stream.Close();
            var st = new StringContent(json, Encoding.UTF8, "text/plain");
            return new HttpResponseMessage() { Content = st };
        }


I have this code but it does not read any document from a url  in this case the url variable is "path" but the program thows me an exception

Harini Chellappa [Syncfusion]
Replied On January 6, 2020 09:36 AM UTC

Hi Ricardo, 

Syncfusion Greetings! 

Can you please ensure the path which you are providing is correct? 

Then Please check with below sample code for importing file from url. 

        public HttpResponseMessage ImportFileURL([FromBody]FileUrlInfo param)  
        {  
            try  
            {  
   
                using (WebClient client = new WebClient())  
                {  
                    MemoryStream stream = new MemoryStream(client.DownloadData(param.fileUrl));  
                    Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, Syncfusion.EJ2.DocumentEditor.FormatType.Docx);  
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);  
                    document.Dispose();  
                    stream.Dispose();  
                    return new HttpResponseMessage() { Content = new StringContent(json, Encoding.UTF8, "text/plain") };  
                }  
            }  
            catch (Exception ex)  
            {  
                return new HttpResponseMessage() { Content = new StringContent("", Encoding.UTF8, "text/plain") };  
            }  
        }  
 
If you still face the issue, then please share the document which you are facing issue along with exception stack trace which will be helpful for us to serve you better. 

Regards, 
Harini C 


Ricardo
Replied On January 6, 2020 06:21 PM UTC

thanks it return the iformation but now it does not show it

the console prints well the info but it throws me a syntax error

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StringContent, Headers: { Content-Type: text/plain; charset=utf-8 } 


[this is the error]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data    sfdt-reader.js:37:72

the code to show the info is this:



@model string
@using Syncfusion.EJ2

<div style="display:block;width:100%;height:500px">
    @Html.EJS().DocumentEditorContainer("container").ServiceUrl( Url.Action("OpenWord", "Download", new { area = "OperacionesGenerales" }) ).EnableToolbar(true).ShowPropertiesPane(false).Created("onCreated").Render()
</div>


<script>

    var fileName = "@Model";

    var documenteditor;

    function onCreated() {
        open(fileName);
    }

    function open(fileName) {

        var container = document.getElementById("container").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()

anyway this is a partial view

Suriya Murugan [Syncfusion]
Replied On January 7, 2020 01:25 PM UTC

Hi Richardo, 

We suspect the issue because of server-side return json is empty or json is not proper string format. Can you please check and confirm server return Json is not empty? In mean time, Can you please share the document which you tried to open? that will helpful for us to investigate further and provide you solution at earliest.   

Regards, 
Suriya M. 


Ricardo
Replied On January 7, 2020 04:12 PM UTC

just replace openword request with a url because Im working with a couchDB database


Attachment: document_doc_573020ea.zip

Harini Chellappa [Syncfusion]
Replied On January 8, 2020 01:33 PM UTC

Hi Richardo, 

Syncfusion Greetings! 

We have a working sample for opening document from URL in Web Forms. Please check it from the below link. 


Sample Document URL 

Also, we suspect there may some issues in document parsing. if possible, can you please download the document from URL and share the document? Else we can schedule for a meeting. 

Regards, 
Harini C 


Ricardo
Replied On January 8, 2020 03:38 PM UTC

Thanks but It did not work I does not resolve the url issue, It has a fatal error that does not compile and finally the sample you gave me is in core but the problem I gave you is in mvc 5

Harini Chellappa [Syncfusion]
Replied On January 9, 2020 07:12 AM UTC

Hi Ricardo, 

For testing, we have shared the already created workable sample. we have prepared the sample with your provided codes in MVC. Kindly check the attached sample from below link. 


kindly check the below OpenWord API code. Instead of returning httpresponsemessage, return the serialized json and check. 

[HttpGet] 
        public string OpenWord(string fileName) 
        { 
 
            //var path = Constantes.Coucher_no_Auth + fileName; 
 
            try 
            { 
 
                using (WebClient client = new WebClient()) 
                { 
                    MemoryStream stream = new MemoryStream(client.DownloadData(fileName)); 
                    WordDocument document = WordDocument.Load(stream, FormatType.Docx); 
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); 
                    document.Dispose(); 
                    stream.Dispose(); 
                    return json; 
                    //var st = new StringContent(json, Encoding.UTF8, "text/plain"); 
                    //return new HttpResponseMessage() { Content = st }; 
                } 
            } 
            catch (Exception) 
            { 
                return "Failure"; 
            } 
 
        } 

If you still face the issue, we can arrange for a meeting. 
Regards, 
Harini C 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon