Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
28 changes: 28 additions & 0 deletions Word-document/OpenAndSave-using-Node-js/CSharpToNodeJS/Sample.js
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
}
28 changes: 28 additions & 0 deletions Word-document/OpenAndSave-using-Node-js/WebApplication1/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
69 changes: 69 additions & 0 deletions Word-document/OpenAndSave-using-Node-js/WebApplication1/Startup.cs
Original file line number Diff line number Diff line change
@@ -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<GzipCompressionProviderOptions>(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");
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.13" />
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="27.2.4" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@WebApplication1_HostAddress = http://localhost:5083

GET {{WebApplication1_HostAddress}}/weatherforecast/
Accept: application/json

###
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5083"
}
}
}
}
Loading