diff --git a/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Input.docx b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Input.docx new file mode 100644 index 00000000..96fa191f Binary files /dev/null and b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Input.docx differ diff --git a/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Sample.js b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Sample.js new file mode 100644 index 00000000..945cdc84 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Sample.js @@ -0,0 +1,28 @@ +const fs = require('fs'); +const FormData = require('form-data'); +const axios = require('axios'); +const https = require('https'); + +const filePath1 = "Input.docx"; // Replace with the actual file path +const fileData1 = fs.readFileSync(filePath1); + +const formData = new FormData(); +formData.append('file', fileData1, { + filename: 'Input.docx', + contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' +}); + +const httpsAgent = new https.Agent({ rejectUnauthorized: false }); + +axios.post('http://localhost:5083/api/docio/OpenAndResave', formData, { + headers: formData.getHeaders(), + responseType: 'arraybuffer', // Ensure response is treated as a binary file + httpsAgent, +}) + .then(response => { + console.log('File successfully processed'); + fs.writeFileSync('ResavedDocument.docx', response.data); + }) + .catch(error => { + console.error('Error:', error.message); + }); diff --git a/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/package.json b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/package.json new file mode 100644 index 00000000..eec84d7d --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/package.json @@ -0,0 +1,17 @@ +{ + "name": "node", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "serve": "app.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "axios": "^1.4.0", + "express": "^4.18.2", + "form-data": "^4.0.0", + "fs": "^0.0.1-security" + } +} diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/Controllers/DocIOController.cs b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Controllers/DocIOController.cs new file mode 100644 index 00000000..3ea48a0f --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Controllers/DocIOController.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Cors; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +namespace WebApplication1.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DocIOController : Controller + { + [HttpPost] + [EnableCors("AllowAllOrigins")] + [Route("OpenAndResave")] + public IActionResult OpenAndResave(IFormFile file) + { + if (file == null || file.Length == 0) + return BadRequest("Invalid file uploaded"); + + try + { + using (Stream inputStream = file.OpenReadStream()) + { + // Open the Word document + using (WordDocument document = new WordDocument(inputStream, FormatType.Docx)) + { + using (MemoryStream memoryStream = new MemoryStream()) + { + // Save the document to the memory stream + document.Save(memoryStream, FormatType.Docx); + + // Reset the stream position to the beginning + memoryStream.Position = 0; + // Convert MemoryStream to Byte Array + byte[] fileBytes = memoryStream.ToArray(); + // Return the file as a downloadable response + return File(memoryStream.ToArray(), + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "ResavedDocument.docx"); + } + } + } + } + catch (Exception ex) + { + return StatusCode(500, $"Error processing document: {ex.Message}"); + } + } + } +} diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/Program.cs b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Program.cs new file mode 100644 index 00000000..225df662 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Program.cs @@ -0,0 +1,28 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add CORS policy +builder.Services.AddCors(options => +{ + options.AddPolicy("AllowAllOrigins", policy => + policy.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader()); +}); + +builder.Services.AddControllers(); + +var app = builder.Build(); + +app.UseRouting(); + +// Use CORS **before** UseAuthorization & UseEndpoints +app.UseCors("AllowAllOrigins"); + +app.UseAuthorization(); + +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); +}); + +app.Run(); diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/Properties/launchSettings.json b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Properties/launchSettings.json new file mode 100644 index 00000000..4d5226af --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:5083", + "sslPort": 44375 + } + }, + "profiles": { + "WebApplication1": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5083", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/Startup.cs b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Startup.cs new file mode 100644 index 00000000..9776613a --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/Startup.cs @@ -0,0 +1,69 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.ResponseCompression; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json; + +namespace WebApplication1 +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + readonly string MyAllowSpecificOrigins = "MyPolicy"; + + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + services.AddMemoryCache(); + + services.AddControllers().AddNewtonsoftJson(options => + { + options.SerializerSettings.ContractResolver = new DefaultContractResolver(); + }); + + // Enable CORS + services.AddCors(options => + { + options.AddPolicy(MyAllowSpecificOrigins, builder => + { + builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }); + }); + + // Response Compression + services.Configure(options => + options.Level = System.IO.Compression.CompressionLevel.Optimal); + services.AddResponseCompression(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseHsts(); + } + + app.UseHttpsRedirection(); + app.UseRouting(); + app.UseAuthorization(); + app.UseCors(MyAllowSpecificOrigins); + app.UseResponseCompression(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers().RequireCors("MyPolicy"); + }); + } + } +} diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj new file mode 100644 index 00000000..d19a0d98 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj.user b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj.user new file mode 100644 index 00000000..9ff5820a --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.csproj.user @@ -0,0 +1,6 @@ + + + + https + + \ No newline at end of file diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.http b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.http new file mode 100644 index 00000000..8bbe9858 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.http @@ -0,0 +1,6 @@ +@WebApplication1_HostAddress = http://localhost:5083 + +GET {{WebApplication1_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.sln b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.sln new file mode 100644 index 00000000..2bd1da18 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/WebApplication1.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35527.113 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication1", "WebApplication1.csproj", "{645B077D-AB30-4DEF-8624-E51914D7125C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {645B077D-AB30-4DEF-8624-E51914D7125C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {645B077D-AB30-4DEF-8624-E51914D7125C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {645B077D-AB30-4DEF-8624-E51914D7125C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {645B077D-AB30-4DEF-8624-E51914D7125C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.Development.json b/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.json b/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.json new file mode 100644 index 00000000..1dd90332 --- /dev/null +++ b/Word-document/OpenAndSave-using-Node-js/WebApplication1/appsettings.json @@ -0,0 +1,10 @@ +{ + "AllowedHosts": "*", + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "http://localhost:5083" + } + } + } +} \ No newline at end of file