RichTextEditor Insert and Update Image name
I am trying to change the image file name for RichTextEditor to do this I use:
<RichTextEditorImageSettings AllowedTypes="@_allowedExtensions" SaveUrl="@_saveUrl" Path="./newsMedia/"/>
<RichTextEditorEvents OnImageUploadSuccess="UploadImageSuccess" ></RichTextEditorEvents>
@code {
private void UploadImageSuccess(ImageSuccessEventArgs args)
{
var headers = args.Response.Headers;
Header = headers.Split("name: ");
Header = Header[1].Split("\r");
args.File.Name = Header[0];
}
}
[HttpPost]
[Route("save")]
public async Task Save(IList<IFormFile> UploadFiles)
{
try
{
foreach (var imageFile in UploadFiles)
{
// Check each image extension
_imageService.CheckImageExtension(imageFile);
// Process image if it has content
if (imageFile.Length > 0)
{
using var memoryStream = new MemoryStream();
await imageFile.CopyToAsync(memoryStream);
var imageBytes = memoryStream.ToArray();
// Save image and get the filename
var imageFilename = await _imageService.SaveImageAsync(imageBytes);
// Set the filename in the response header
Response.Headers.Add("name", imageFilename);
Response.StatusCode = 200;
Response.ContentType = "application/json; charset=utf-8";
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
// Set an appropriate status code for errors
Response.StatusCode = StatusCodes.Status500InternalServerError;
}
}
But in args.Response.Headers I get empty headers, although in the Network tab in the browser I can see that I have a header with "Name". Maybe it's because I have Client and Server on different ports
Is there any way to fix this?
Hi Alexandr Cutar,
We have reviewed the sample provided and found that the issue with obtaining headers in the UploadImageSuccess event was not replicated. This suggests that the configuration in your application might need some adjustments.
To ensure proper handling of API responses, including headers, please verify the following settings in your Program.cs file:
- HttpClient Configuration: Ensure that
HttpClientis properly configured to handle API calls. For example:
|
- CORS Configuration: If your API is hosted on a different domain, you need to configure CORS to allow headers. You can do this in the
Program.csfile of your API project:
|
For more information on calling a web API from an ASP.NET Core Blazor app, you can refer to the Microsoft Learn article.
Here's the revised response with the attached sample content mentioned:
Attachment: SyncfusionBlazorApp1_a04c6e6a.zip
Regards,
Dhanesh Priya Sankar
I did it.
My Program.cs in API project
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder.WithOrigins("https://localhost:7194")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
My Program.cs in Blazor project
builder.Services.AddScoped(sp =>newHttpClient{BaseAddress=newUri("https://localhost:7003")});
The problem remains
I found a solution by adding a method to the CORS configuration
.WithExposedHeaders("name");
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policyBuilder =>
{
policyBuilder.WithOrigins("https://your-blazor-app-url.com")
.AllowAnyHeader()
.AllowAnyMethod()
.WithExposedHeaders("name");
});
});
Hi Alexandr Cutar,
We are glad that you have found the solution. Please let us know if you need any further assistance.
Regards,
Vinothkumar
- 4 Replies
- 3 Participants
- Marked answer
-
RD RTi Developer
- Sep 15, 2024 05:22 PM UTC
- Sep 17, 2024 09:58 AM UTC