Код: Выделить всё
[HttpGet("GetMediaVal/{id}")]
public async Task GetMediaValue(Guid id)
{
SqlConnection con = new SqlConnection(_connectionString);
try
{
await con.OpenAsync();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "SELECT TOP 1 med_ext, med_val FROM media WHERE id = @id";
cmd.Parameters.AddWithValue("id", id);
SqlDataReader reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.SingleRow | CommandBehavior.CloseConnection);
if (!await reader.ReadAsync())
return NotFound();
string med_ext = reader.GetString(0);
if (String.IsNullOrWhiteSpace(med_ext) || !new FileExtensionContentTypeProvider().TryGetContentType(Path.ChangeExtension("file", med_ext), out string contentType))
contentType = "application/octet-stream"; // Fallback
if (reader.IsDBNull(1))
return NotFound();
Stream s = reader.GetStream(1);
return File(s, contentType, true);
}
}
catch (Exception ex)
{
await con.CloseAsync();
return StatusCode((int)HttpStatusCode.InternalServerError, "Error");
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... -via-strea
Мобильная версия