Skip to content

Commit c32a3c0

Browse files
ES-942032-Node-JS
1 parent 6895d2e commit c32a3c0

File tree

13 files changed

+287
-0
lines changed

13 files changed

+287
-0
lines changed
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fs = require('fs');
2+
const FormData = require('form-data');
3+
const axios = require('axios');
4+
const https = require('https');
5+
6+
const filePath1 = "Input.docx"; // Replace with the actual file path
7+
const fileData1 = fs.readFileSync(filePath1);
8+
9+
const formData = new FormData();
10+
formData.append('file', fileData1, {
11+
filename: 'Input.docx',
12+
contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
13+
});
14+
15+
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
16+
17+
axios.post('http://localhost:5083/api/docio/OpenAndResave', formData, {
18+
headers: formData.getHeaders(),
19+
responseType: 'arraybuffer', // Ensure response is treated as a binary file
20+
httpsAgent,
21+
})
22+
.then(response => {
23+
console.log('File successfully processed');
24+
fs.writeFileSync('ResavedDocument.docx', response.data);
25+
})
26+
.catch(error => {
27+
console.error('Error:', error.message);
28+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"serve": "app.js"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"axios": "^1.4.0",
13+
"express": "^4.18.2",
14+
"form-data": "^4.0.0",
15+
"fs": "^0.0.1-security"
16+
}
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Cors;
3+
using Syncfusion.DocIO;
4+
using Syncfusion.DocIO.DLS;
5+
6+
namespace WebApplication1.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class DocIOController : Controller
11+
{
12+
[HttpPost]
13+
[EnableCors("AllowAllOrigins")]
14+
[Route("OpenAndResave")]
15+
public IActionResult OpenAndResave(IFormFile file)
16+
{
17+
if (file == null || file.Length == 0)
18+
return BadRequest("Invalid file uploaded");
19+
20+
try
21+
{
22+
using (Stream inputStream = file.OpenReadStream())
23+
{
24+
// Open the Word document
25+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
26+
{
27+
using (MemoryStream memoryStream = new MemoryStream())
28+
{
29+
// Save the document to the memory stream
30+
document.Save(memoryStream, FormatType.Docx);
31+
32+
// Reset the stream position to the beginning
33+
memoryStream.Position = 0;
34+
// Convert MemoryStream to Byte Array
35+
byte[] fileBytes = memoryStream.ToArray();
36+
// Return the file as a downloadable response
37+
return File(memoryStream.ToArray(),
38+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
39+
"ResavedDocument.docx");
40+
}
41+
}
42+
}
43+
}
44+
catch (Exception ex)
45+
{
46+
return StatusCode(500, $"Error processing document: {ex.Message}");
47+
}
48+
}
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add CORS policy
4+
builder.Services.AddCors(options =>
5+
{
6+
options.AddPolicy("AllowAllOrigins", policy =>
7+
policy.AllowAnyOrigin()
8+
.AllowAnyMethod()
9+
.AllowAnyHeader());
10+
});
11+
12+
builder.Services.AddControllers();
13+
14+
var app = builder.Build();
15+
16+
app.UseRouting();
17+
18+
// Use CORS **before** UseAuthorization & UseEndpoints
19+
app.UseCors("AllowAllOrigins");
20+
21+
app.UseAuthorization();
22+
23+
app.UseEndpoints(endpoints =>
24+
{
25+
endpoints.MapControllers();
26+
});
27+
28+
app.Run();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:5083",
8+
"sslPort": 44375
9+
}
10+
},
11+
"profiles": {
12+
"WebApplication1": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": false,
16+
"applicationUrl": "http://localhost:5083",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"IIS Express": {
22+
"commandName": "IISExpress",
23+
"launchBrowser": true,
24+
"environmentVariables": {
25+
"ASPNETCORE_ENVIRONMENT": "Development"
26+
}
27+
}
28+
}
29+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.ResponseCompression;
3+
using Newtonsoft.Json.Serialization;
4+
using Newtonsoft.Json;
5+
6+
namespace WebApplication1
7+
{
8+
public class Startup
9+
{
10+
public Startup(IConfiguration configuration)
11+
{
12+
Configuration = configuration;
13+
}
14+
15+
public IConfiguration Configuration { get; }
16+
17+
readonly string MyAllowSpecificOrigins = "MyPolicy";
18+
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddControllers();
22+
services.AddMemoryCache();
23+
24+
services.AddControllers().AddNewtonsoftJson(options =>
25+
{
26+
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
27+
});
28+
29+
// Enable CORS
30+
services.AddCors(options =>
31+
{
32+
options.AddPolicy(MyAllowSpecificOrigins, builder =>
33+
{
34+
builder.AllowAnyOrigin()
35+
.AllowAnyMethod()
36+
.AllowAnyHeader();
37+
});
38+
});
39+
40+
// Response Compression
41+
services.Configure<GzipCompressionProviderOptions>(options =>
42+
options.Level = System.IO.Compression.CompressionLevel.Optimal);
43+
services.AddResponseCompression();
44+
}
45+
46+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
47+
{
48+
if (env.IsDevelopment())
49+
{
50+
app.UseDeveloperExceptionPage();
51+
}
52+
else
53+
{
54+
app.UseHsts();
55+
}
56+
57+
app.UseHttpsRedirection();
58+
app.UseRouting();
59+
app.UseAuthorization();
60+
app.UseCors(MyAllowSpecificOrigins);
61+
app.UseResponseCompression();
62+
63+
app.UseEndpoints(endpoints =>
64+
{
65+
endpoints.MapControllers().RequireCors("MyPolicy");
66+
});
67+
}
68+
}
69+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.13" />
11+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="27.2.4" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>https</ActiveDebugProfile>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@WebApplication1_HostAddress = http://localhost:5083
2+
3+
GET {{WebApplication1_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication1", "WebApplication1.csproj", "{645B077D-AB30-4DEF-8624-E51914D7125C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{645B077D-AB30-4DEF-8624-E51914D7125C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{645B077D-AB30-4DEF-8624-E51914D7125C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{645B077D-AB30-4DEF-8624-E51914D7125C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{645B077D-AB30-4DEF-8624-E51914D7125C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"AllowedHosts": "*",
3+
"Kestrel": {
4+
"Endpoints": {
5+
"Http": {
6+
"Url": "http://localhost:5083"
7+
}
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)