You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Web API返回Zip文件夹时出现500内部服务器错误,代码运行无异常

问题:ASP.NET Core Web API返回Zip文件夹时触发500内部服务器错误

我在使用ASP.NET Core Web API时遇到问题:无法返回Zip文件夹,触发500内部服务器错误,但代码运行顺畅。

Azure Function中的API代码

[FunctionName(nameof(GetPackage))]
[OpenApiOperation(operationId: "get-package", tags: new[] { "asap" }, Summary = "Get Package")]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(UpdateSDRequestInputModel), Required = true, Description = "Get Package")]
public async Task<IActionResult> GetPackage(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "v1/getPackage/{alias}/{jobId}")] HttpRequest req, 
    [FromUri] string alias, [FromUri] string jobId, Microsoft.Azure.WebJobs.ExecutionContext executionContext)
{
    return await ExecuteAndFormResponseAsync(req, executionContext.FunctionName, async () =>
    {
        return new OkObjectResult(await _qPPService.GetPackage(alias, jobId,req));
    });
}

GetPackage方法代码

public async Task<FileStreamResult> GetPackage(string alias, string jobId)
{
    try
    {
        string _authToken = await GetAuthToken();

        _httpClient.DefaultRequestHeaders.Clear();
        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _authToken);

        var addtnlRequest = await _additionalRequestRepository.GetAdditionalRequestAsync($"Additional-{jobId}", alias)
            ?? throw new NotFoundException($"Request with Id: Additional-{jobId} for alias: {alias} does not exists");

        var request = await _requestRepository.GetRequestAsync($"Primary-{jobId}", alias) ??
            throw new NotFoundException($"Request with Id: Additional-{jobId} for alias: {alias} does not exists");

        var requestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(_qPPEndPoint + "jobs/" + addtnlRequest.QPPRequestId + "/downloads/files?path=" + "3c35af-FullPackage-2308171550.zip")
            };

        var responseMessage = await _httpClient.SendAsync(requestMessage);
        responseMessage.EnsureSuccessStatusCode();

        Stream stream = responseMessage.Content.ReadAsStream();

         responseMessage.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");

         return new FileStreamResult(stream, "application/zip")
                {
                    FileDownloadName = "3c35af-FullPackage-2308171550.zip"
                };
    }
    catch (Exception ex)
    {
        ExceptionHelper.LogAndThrowException($"Failed getting package for {jobId} {Environment.NewLine} {ex}", _logAndTelemetryHelper);
        throw;
    }
}

内容的提问来源于stack exchange,提问作者Ramanjaneya Shetty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:22:41