我需要通过WebApi从sql Server传输blob数据.
我不想在Web服务器上缓冲内存中的blob数据.
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将以下错误写为reposnse:
[fiddler] ReadResponse()失败:服务器未返回此请求的响应.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。