FileSqlServerSync/Server/Common/ProgressStreamContent.cs
ZhaoLei fa64f0c6d5 fix&feat: c# 上传文件存在最大限制。消息优化和更改,现在减少用户使用时的担心,它更加人性化
```csharp
// 使用默认的Kestrel  服务器部署,将默认限制最大文件的上传,使用这个取消限制
 [DisableRequestSizeLimit]
 [HttpPost("/UploadFile")]
 public async Task<IActionResult> UploadFile(IFormFile file)
 {

```
2024-10-29 18:08:34 +08:00

35 lines
1,005 B
C#

using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
namespace Common;
public class ProgressStreamContent(Stream stream_, IProgress<double> progress)
: StreamContent(stream_, 5 * 1024 * 1024)
{
private readonly Stream FileStream = stream_;
private readonly int BufferSize = 5 * 1024 * 1024;
private readonly IProgress<double> Progress = progress;
protected override async Task SerializeToStreamAsync(
Stream stream,
TransportContext? context = null
)
{
var buffer = new byte[BufferSize];
long totalBytesRead = 0;
long totalBytes = FileStream.Length;
int bytesRead;
while ((bytesRead = await FileStream.ReadAsync(buffer.AsMemory())) != 0)
{
await stream.WriteAsync(buffer.AsMemory(0, bytesRead));
totalBytesRead += bytesRead;
Progress.Report((double)totalBytesRead / totalBytes);
}
}
}