I need to stream blob data from Sql Server via WebApi.
I DONT want to buffer the blob data in memory on the web server.
I have the following code, but it does not work - there is NO exception.
public class AttachmentController : ApiController
{
public async Task<HttpResponseMessage> Get(int id)
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
await connection.OpenAsync();
using (var command = new SqlCommand("SELECT Content FROM [Attachments] WHERE ID = @ID", connection))
{
command.Parameters.AddWithValue("ID", id);
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
if (await reader.ReadAsync())
{
using (Stream data = reader.GetStream(0))
{
var response = new HttpResponseMessage{Content = new StreamContent(data)};
//I get this from the DB else where
//response.Content.Headers.ContentType = new MediaTypeHeaderValue(attachment.ContentType);
//I get this from the DB else where
//response.Content.Headers.ContentLength = attachment.ContentLength;
return response;
}
}
}
}
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
}
Fiddle writes the following error as the reposnse: [Fiddler] ReadResponse() failed: The server did not return a response for this request.
How can I stream the content from the DB to the http output stream, with out buffering it in memory?