Код: Выделить всё
[HttpGet("{fileName}")]
public async Task Get(string fileName)
{
var memoryStream = new MemoryStream();
try
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ApplicationException("FileName is required as a path parameter.");
}
string fullPathName = Path.Combine(_settings.UploadRootFolder!, fileName);
if (!System.IO.File.Exists(fullPathName))
{
throw new ApplicationException($"{fileName} does not exist in {_settings.UploadRootFolder}.");
}
var fi = new FileInfo(fullPathName);
var contentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
//ModificationDate = fi.LastAccessTimeUtc,
FileName = fileName,
FileNameStar = fileName,
Size = fi.Length,
//CreationDate = fi.CreationTimeUtc,
//DispositionType = "attachment",
//ReadDate = DateTime.UtcNow,
};
var mem = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64;
var memKB = mem / 1024;
Response.Headers.ContentDisposition = contentDisposition.ToString();
_logger.LogInformation($"Attempting to download {fileName} from {_settings.UploadRootFolder} using {memKB.ToString("n0")} KB memory.");
using (var fileStream = new FileStream(fullPathName, FileMode.Open, FileAccess.Read))
{
await fileStream.CopyToAsync(memoryStream, 64 * 1024);
memoryStream.Position = 0;
return new FileStreamResult(memoryStream, MimeTypes.GetMimeType(fileName));
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception trying to download a file");
return BadRequest();
}
}

В моем клиенте (Blazor) я вызываю загрузку с помощью этого кода:
Код: Выделить всё
private async Task btnDownload_Clicked(string fileName)
{
if (!string.IsNullOrWhiteSpace(fileName))
{
try
{
var response = await _apiClient.GetAsync($"/api/FileDownload/{fileName}");
response.EnsureSuccessStatusCode();
//
// Even though the API successfully set the ContentDisposition header
// (verified using debugger) it is null here on the client.
//
// var serverFileName = response.Content.Headers.ContentDisposition.FileName;
var serverFileName = fileName;
using (var stremRef = new DotNetStreamReference(stream: response.Content.ReadAsStream()))
{
await JS.InvokeVoidAsync("downloadFileFromStream", serverFileName, stremRef);
}
}
catch (Exception ex)
{
JS.InvokeVoidAsync("alert", ex.Message);
throw;
}
}
}
Я должен иметь возможность установить serverFileName на имя, найденное в Content-Disposition, но оно всегда равно NULL.

Когда я закомментирую строку, чтобы получить имя файла из заголовка, и просто использую аргумент Я отправил путь, загрузка прошла успешно, однако мне нужно использовать Content-Disposition.
Любая помощь приветствуется, мой API работает на https:/ /localhost:7283, и мой клиент работает на https://localhost:7203, поэтому они НЕ являются приложением Blazor, загруженным из модели клиент/сервер, которую VS рекомендует в качестве шаблона (отдельный клиент и сервер). сайты кажутся более реальными).
Спасибо за любую помощь!
Подробнее здесь: https://stackoverflow.com/questions/791 ... ion-header