Save file on server side in docx format

Hello, I understand how to download files to client side but how can I save file to server side, for example /files dir and save filename to my db?  

1 Reply

HC Harini Chellappa Syncfusion Team February 27, 2020 06:11 AM UTC

Hi Olga, 

Syncfusion Greetings! 

Document editor provides following two options to pass the document editor content to server-side. 
1.       Blob 
2.       SFDT 

You can either pass the document as blob or SFDT to server-side Web API. 

Exporting as blob 

If you are sending the content as blob, you need to use the DOCIO for saving the blob content in server side as docx document. 

Please refer the below UG documentation on saving the blob content as RTF document. You can just change the format type to ‘Docx’ from ‘Rtf’  


Server-side Web API Code 

[HttpPost] 
public HttpResponseMessage ExportAsRtf() 
{ 
    System.Web.HttpPostedFile data = HttpContext.Current.Request.Files[0]; 
    //Opens document stream 
    Syncfusion.DocIO.DLS.WordDocument wordDocument = new Syncfusion.DocIO.DLS.WordDocument(data.InputStream); 
    MemoryStream stream = new MemoryStream(); 
    //Converts document stream as RTF 
    wordDocument.Save(stream, FormatType.Docx); 
    wordDocument.Close(); 
    stream.Position = 0; 
    return new HttpResponseMessage() { Content = new StreamContent(stream) }; 
} 
 

You can also refer the below KB documentations platform-wise which helps you to save the document in server.  



Export as SFDT 

To get the current document content as SFDT string, please use the following API. 

containerInstance.documentEditor.serialize() 
 
Client-side http request 

    let http: XMLHttpRequest = new XMLHttpRequest(); 
    http.open('POST', 'http://localhost:5000/api/documenteditor/ExportSFDT'); 
    http.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); 
    http.responseType = 'json'; 
    let sfdt: any = { content: container.documentEditor.serialize() }; 
    http.send(JSON.stringify(sfdt)); 
 

Server-side Web API code 

[AcceptVerbs("Post")] 
        [HttpPost] 
        [EnableCors("AllowAllOrigins")] 
        [Route("ExportSFDT")] 
        public string ExportSFDT([FromBody]SaveParameter data) 
        { 
            Stream document = Syncfusion.EJ2.DocumentEditor.WordDocument.Save(data.content, Syncfusion.EJ2.DocumentEditor.FormatType.Docx); 
            string path = _hostingEnvironment.ContentRootPath; 
            FileStream file = new FileStream(path + "sample1.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
            document.CopyTo(file); 
            file.Close(); 
            document.Close(); 
            document.Position = 0; 
            return "Sucess"; 
        } 
public class SaveParameter 
        { 
            public string content { get; set; } 
        } 
 

Please check the above and let us know whether this helps you. 

Regards, 
Harini C 


Loader.
Up arrow icon