如何使用.NET Core Web API下载Angular的大文件(最低1 GB)?

发布于 2025-01-31 09:17:27 字数 1764 浏览 3 评论 0原文

我一直在开发一个需要下载文件的系统。文件的大小至少为1 GB。在本地计算机上,下载文件几乎不花10秒钟,但是当我在服务器上执行相同的操作时,下载需要超过2个小时才能下载。

我已经使用了Angular进行forntend,在后端有.NET CORE 5。

下面是角度代码。

 public GetFile<R>(actionName: string, param: any = null): any {
    let response = null;
    const uri = `${this.BaseUrl}${actionName}?keyname=${param.keyname}`; 
    const options: Object = {
      headers: new HttpHeaders({'Content-Type': 'application/json'}),
      observe: "body",
      reportProgress: true,
      responseType: "blob"
    };
    
    response = this.http.patch<R>(uri, param, options)
    .pipe(
      catchError(this.handleError<string>(`Get file [${actionName}] failed`))
    );
    return response;
  }

以下是.NET核心代码。

[HttpPatch]
[Route("DownloadJobResult")]
public async Task<ActionResult> DownloadJobResult(string keyname)
{
    IScheduler scheduler = await _factory.GetScheduler();

    JobKey key = new JobKey(keyname);

    IJobDetail result = await scheduler.GetJobDetail(key);

    string physicalPath = result.JobDataMap.GetString("FILENAME_ON_DISK");
    string downloadFileName = result.JobDataMap.GetString("DOWNLOAD_FILENAME");

    if (string.IsNullOrEmpty(physicalPath) || string.IsNullOrEmpty(downloadFileName))
        return NotFound();

    var memory = new MemoryStream();
    using (var stream = new FileStream(physicalPath, FileMode.Open))
    {
        await stream.CopyToAsync(memory);
    }
    memory.Position = 0;

    return File(memory, GetContentType(physicalPath), downloadFileName);
}

在这两个地方,都有相同的代码。在本地和服务器上。但是我无法弄清楚为什么从服务器下载花费太多时间。

这是服务器信息:

OS: Windows Server 2019
Memory: 8 GB
Storage: 100 GB

请提供帮助。

让我知道您是否需要更多信息。

I have been developing a system that needs to download files. A file is at least 1 GB in size. On my local machine, it took me hardly 10 seconds to download the file but when I perform the same operation on the server, it takes more than 2 hours to download.

I have used angular for forntend and in backend there is .net core 5.

Below is the angular code.

 public GetFile<R>(actionName: string, param: any = null): any {
    let response = null;
    const uri = `${this.BaseUrl}${actionName}?keyname=${param.keyname}`; 
    const options: Object = {
      headers: new HttpHeaders({'Content-Type': 'application/json'}),
      observe: "body",
      reportProgress: true,
      responseType: "blob"
    };
    
    response = this.http.patch<R>(uri, param, options)
    .pipe(
      catchError(this.handleError<string>(`Get file [${actionName}] failed`))
    );
    return response;
  }

Below is the .net core code.

[HttpPatch]
[Route("DownloadJobResult")]
public async Task<ActionResult> DownloadJobResult(string keyname)
{
    IScheduler scheduler = await _factory.GetScheduler();

    JobKey key = new JobKey(keyname);

    IJobDetail result = await scheduler.GetJobDetail(key);

    string physicalPath = result.JobDataMap.GetString("FILENAME_ON_DISK");
    string downloadFileName = result.JobDataMap.GetString("DOWNLOAD_FILENAME");

    if (string.IsNullOrEmpty(physicalPath) || string.IsNullOrEmpty(downloadFileName))
        return NotFound();

    var memory = new MemoryStream();
    using (var stream = new FileStream(physicalPath, FileMode.Open))
    {
        await stream.CopyToAsync(memory);
    }
    memory.Position = 0;

    return File(memory, GetContentType(physicalPath), downloadFileName);
}

In both places, there is the same code. On local and on the server as well. But I cannot figure out why it takes too much time to download from the server.

This is the server info:

OS: Windows Server 2019
Memory: 8 GB
Storage: 100 GB

Please help.

Let me know if you need some more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文