C
C#2y ago
paulman176

❔ Unable to process post request with asp.net core

I am making use of a document processing component (https://ej2.syncfusion.com/react/documentation/document-editor/web-services/core), where i need to use asp.net core for some server dependencies such as importing a docx file into the documenteditor. My problem is that I don't know wheter my fetch method is not working (and maybe wrong usage of my endpoints) or that the asp.net core application is not even running correctly
5 Replies
paulman176
paulman176OP2y ago
when i typed dotnet run it said:
Now listening on: http://localhost:5179
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/paulvanmierlo/Projects/Contractmanagement/Contractmanagement
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
Now listening on: http://localhost:5179
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/paulvanmierlo/Projects/Contractmanagement/Contractmanagement
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
paulman176
paulman176OP2y ago
i succesfully figured out that the http responses are working because i created a simple get request to make sure i am able to get some response from my server ( a simple OK message) Tried it with swagger and postman and both responded with OK so maybe there is something wrong with how i am fetching the data? also this is my console log from within the browser:
OK response received
OK response received
for the sample get request i am using the same port as URL for the get request as for the post request i am still not able to perform this is the controller code:
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.EJ2.DocumentEditor;
using System.IO;

[ApiController]
[Route("[controller]")]
public class ImportController : ControllerBase
{

[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Import")]
public string Import(IFormCollection data)
{
if (data.Files.Count == 0)
return null;
Stream stream = new MemoryStream();
IFormFile file = data.Files[0];
int index = file.FileName.LastIndexOf('.');
string type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";
file.CopyTo(stream);
stream.Position = 0;

WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
}

private FormatType GetFormatType(string fileExtension)
{
if (string.IsNullOrEmpty(fileExtension))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (fileExtension)
{
case ".docx":
return FormatType.Docx;
case ".doc":
return FormatType.Doc;
case ".rtf":
return FormatType.Rtf;
case ".txt":
return FormatType.Txt;
// Add more cases for other supported file extensions if needed
default:
return FormatType.Docx; // Default to a specific format if the extension is not recognized
}
}
}
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Syncfusion.EJ2.DocumentEditor;
using System.IO;

[ApiController]
[Route("[controller]")]
public class ImportController : ControllerBase
{

[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("Import")]
public string Import(IFormCollection data)
{
if (data.Files.Count == 0)
return null;
Stream stream = new MemoryStream();
IFormFile file = data.Files[0];
int index = file.FileName.LastIndexOf('.');
string type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";
file.CopyTo(stream);
stream.Position = 0;

WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
}

private FormatType GetFormatType(string fileExtension)
{
if (string.IsNullOrEmpty(fileExtension))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (fileExtension)
{
case ".docx":
return FormatType.Docx;
case ".doc":
return FormatType.Doc;
case ".rtf":
return FormatType.Rtf;
case ".txt":
return FormatType.Txt;
// Add more cases for other supported file extensions if needed
default:
return FormatType.Docx; // Default to a specific format if the extension is not recognized
}
}
}
and this is how i am fetching the data
DocumentEditorComponent.Inject(Toolbar);
export default function Documenteditor() {
// Function to handle the document import
const importDocument = (file) => {
const formData = new FormData();
formData.append('file', file); // Assuming you have a file object from an input field

fetch('https://localhost:7166/Import/Import', {
method: 'POST',
body: formData,
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error importing document');
}
})
.then((document) => {
console.log(document); // Handle the imported document data
})
.catch((error) => {
console.error('Error importing document:', error);
});
};

// Event handler for file input change
const fileInputOnChange = (event) => {
const file = event.target.files[0];
importDocument(file);
};

return (
<>
<input type="file" onChange={fileInputOnChange} />
<DocumentEditorContainerComponent id="container" height={'1000px'} enableToolbar={true}>
<Inject services={[Toolbar]} />
</DocumentEditorContainerComponent>
</>
);
}
DocumentEditorComponent.Inject(Toolbar);
export default function Documenteditor() {
// Function to handle the document import
const importDocument = (file) => {
const formData = new FormData();
formData.append('file', file); // Assuming you have a file object from an input field

fetch('https://localhost:7166/Import/Import', {
method: 'POST',
body: formData,
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error('Error importing document');
}
})
.then((document) => {
console.log(document); // Handle the imported document data
})
.catch((error) => {
console.error('Error importing document:', error);
});
};

// Event handler for file input change
const fileInputOnChange = (event) => {
const file = event.target.files[0];
importDocument(file);
};

return (
<>
<input type="file" onChange={fileInputOnChange} />
<DocumentEditorContainerComponent id="container" height={'1000px'} enableToolbar={true}>
<Inject services={[Toolbar]} />
</DocumentEditorContainerComponent>
</>
);
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server