Nhibernate: saving and retrieving file to / from database

I want to save PDFs to mssql using nhibernate. How should I do it.

I read a little about it here , it says that you can create a type to save the file as:

Map(x => x.Bytes)
  .CustomSqlType("VARBINARY (MAX) FILESTREAM")
  .Length(2147483647)

      

but I still can't figure out what the C # field type should be, and if its not FileInfo / FileStream then how to read the entry into that field.

+3


source to share


1 answer


DB Varbinary type, has a C # representation byte[]

public virtual MyFile byte[] { get; set; }

      



Depending on the type of application (for example, ASP.NET Web API), we may provide "download" functionality, for example

protected virtual HttpResponseMessage Download(string fileName, string contentType
    , byte[] data)
{
    var path = fileName;
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    var stream = new MemoryStream(data);
    result.Content = new StreamContent(stream);
    result.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    result.Content.Headers.Add("Content-Disposition", "attachment; fileName=" + path);

    return result;
}

      

+4


source







All Articles