I have setup a SQL database with FILESTREAM support and am trying to stream files retrieved from the database with SqlFileStream through WebAPI to the browser.
For some reason it does not work, but I get no proper error message. The browser just aborts the connection, Fiddler does not show anything useful either, and no error seems to be thrown in VS.
public HttpResponseMessage Get(Guid id)
{
if(id == null || id == Guid.Empty)
return Request.CreateResponse(HttpStatusCode.BadRequest);
try
{
FileStreamContext fsc = null;
Document document = null;
using(var transaction = new TransactionScope())
using (var db = new MyEntities())
{
try
{
fsc = db.Database.SqlQuery<FileStreamContext>("SELECT [File].PathName() AS InternalPath, GET_FILESTREAM_TRANSACTION_CONTEXT() AS TransactionContext FROM Document WHERE id={0}", id).First();
}
catch (Exception e)
{
Debug.Print(e.ToString());
}
document = db.Documents.Where(doc => doc.ID == id).Single();
var fileStream = new SqlFileStream(fsc.InternalPath, fsc.TransactionContext, FileAccess.Read);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StreamContent(fileStream);
//response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
//response.Content.Headers.ContentDisposition.FileName = document.FileName;
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(document.ContentType);
return response;
}
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
I suspect it might be a problem with the TransactionScope being closed to early? Not sure why I don't get any error messages though.
What is the proper way to stream a SqlFileStream over WebApi?