Question

If anyone can help me figure this out, I would greatly appreciate it. For starters, I have a class like so:

public class Blob
{
    public int BlobID { get; set; }
    public string BlobName { get; set; }
    public string FileName { get; set; }
    public string FileMimeType { get; set; }
    public int FileSize { get; set; }
    public byte[] FileContent{ get; set; }
    public DateTime DateCreated { get; set; }
    public int CreatedByProfileID { get; set; }
}

Pretty standard, it's just an object that maps to a table with the exact same field names. The table in SQL Server looks like this:

enter image description here

My controller has add and view actions to do the reading and writing to the DB. I can write the file fine, using the action code below:

[HttpPost]
public ActionResult Add(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {
        Database db = DatabaseFactory.CreateDatabase("dbconnstr");

        byte[] fileContent = new byte[file.ContentLength];
        file.InputStream.Read(fileContent, 0, file.ContentLength);

        object[] paramaters = 
        {
            file.FileName,
            file.FileName,
            file.ContentType,
            file.ContentLength,
            fileContent,
            DateTime.Now,
            12518
        };

        db.ExecuteNonQuery("sp_Blob_Insert", paramaters);
    }

    return RedirectToAction("Index");
}

But when I use the View action code below to read the file out to the browser, the FileContent field is always null:

public ActionResult View(int id)
{
    Database db = DatabaseFactory.CreateDatabase("dbconnstr");

    Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", id).Single();

    return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}

However, if I specifically map the field name, it works:

public ActionResult View(int id)
{
    Database db = DatabaseFactory.CreateDatabase("dbconnstr");

    IRowMapper<Blob> mapper = MapBuilder<Blob>.MapAllProperties().MapByName(x => x.FileContent).Build();

    Blob blob = db.ExecuteSprocAccessor<Blob>("sp_Blob_SelectByPkValue", mapper, id).Single();

    return File(blob.FileContent, blob.FileMimeType, blob.FileName);
}

Is this a bug with the ExecuteSprocAccessor() function? Am I doing anything wrong?

Thanks for your time in advance.

Was it helpful?

Solution

You can use the following code:

.Map(x => x.FileContent).WithFunc(ConvertVarBinaryToByteArray); 

Then create function like this:

private static byte[] ConvertVarBinaryToByteArray(IDataRecord dataRecord) 
{ 
    return (byte[]) dataRecord.GetValue(dataRecord.GetOrdinal("FileContent")); 
} 

OTHER TIPS

I solved the problem this way:

Add this code before execute ExecuteSprocAccessor:

IRowMapper<FileEmail> rowMapper = MapBuilder<FileEmail>.MapAllProperties()
        .Map(x => x.MyFile)
        .WithFunc(ConvertVarBinaryToByteArray).Build();    

And create the method how Russ said above:

private static byte[] ConvertVarBinaryToByteArray(IDataRecord dataRecord)
    {
        return (byte[])dataRecord.GetValue(dataRecord.GetOrdinal("FileContent"));
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top