0
a.mourad created
public virtual async Task<List<AppFileDescriptorDto>> UploadFilesAsync(List<IRemoteStreamContent> inputs)
{
var uploadedFiles = new List<AppFileDescriptorDto>();
foreach (var input in inputs)
{
var id = GuidGenerator.Create();
var fileDescriptor = new AppFileDescriptors.AppFileDescriptor(id, input.FileName, input.ContentType);
try
{
byte[] fileBytes;
// Read the stream **once** into a byte array
await using (var originalStream = input.GetStream())
{
if (originalStream == null || !originalStream.CanRead)
{
throw new InvalidOperationException("Invalid stream: The stream is null or unreadable.");
}
using (var memoryStream = new MemoryStream())
{
await originalStream.CopyToAsync(memoryStream);
fileBytes = memoryStream.ToArray();
}
}
// Insert file metadata into database
await _appFileDescriptorRepository.InsertAsync(fileDescriptor);
// Create a fresh stream from the byte array for saving
await using (var finalStream = new MemoryStream(fileBytes))
{
await _blobContainer.SaveAsync(fileDescriptor.Id.ToString("N"), finalStream);
}
// Map to DTO
var dto = ObjectMapper.Map<AppFileDescriptors.AppFileDescriptor, AppFileDescriptorDto>(fileDescriptor);
uploadedFiles.Add(dto);
}
catch (Exception ex)
{
Console.WriteLine($"Error processing file {input.FileName}: {ex.Message}");
throw;
}
}
return uploadedFiles;
}
1 Answer(s)
-
0
Hello a.mourad,
IRemoteStreamContent
interface is as follows:public interface IRemoteStreamContent : IDisposable { string? FileName { get; } string ContentType { get; } long? ContentLength { get; } Stream GetStream(); }
You can see that it contains streams.
Actually the exception is not thrown by ABP but by .NET. Here is why: https://stackoverflow.com/questions/55674322/getting-the-inner-stream-position-has-changed-unexpectedly-in-aws-lambda/55674833#55674833
Here's where the exception was thrown: https://github.com/dotnet/aspnetcore/blob/81b289460a9ce5fcbd0d32d80a645f139dffc422/src/Http/Http/src/Internal/ReferenceReadStream.cs#L70