Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:46:04+00:00 2026-05-28T17:46:04+00:00

For an asp.Net MVC project, I will need to handle large files( mostly 200-300Mo,

  • 0

For an asp.Net MVC project, I will need to handle large files( mostly 200-300Mo, sometime 1Go).

I will store them in the database(for backup reasons/consistency reason).

I’m concerned by performance issue, so I want to avoid all I can to have a array of byte anywhere in the program, the goal is then to work with stream every where.

I’ve a layered application, which mostly means that I’ve several “DataStore”, which are responsible to connect and retrieve/insert/update data from the database.

Since EF doesn’t support Filestream for now, I’m handling the “File part” through simple Sql requests. I’ve read a good article on filestream usage here: http://blog.tallan.com/2011/08/22/using-sqlfilestream-with-c-to-access-sql-server-filestream-data/

And I’ve some additional questions, which I hope you can help me/point me to the good direction:

  • Since I’ve a layered application, once I’ve my SQLFileStream object instantiated, could I dispose SqlCommand/Sql Connection/Transaction scope?
  • If not, how I’m supposed to close them?
  • In the previous link, there is an example which show how to use it with ASP. But since I’m using ASP.Net MVC, isn’t there an helper which is directly able to stream a file to the browser? Because I found many example of return binary data to browser, but for now, all example I found make basically something like Stream.ToArray() to fill an array of byte and return it to the browser. I found that I can return a FileStreamResult which can take in parameter a Stream. Is that the right direction?

(I’m not currently concerned by uploading big files, since they are inserted by an heavy client in the database)

EDIT

(Sorry for the dirty code, it’s only to not have 50 different methods here.
I’ve made a few more try, and I’m currently stuck with the “read” part, because of separated part(where we generate the layer and where we consume it):

        SqlConnection conn = GetConnection();
        conn.Open();
        SqlCommand cmd = new SqlCommand(_selectMetaDataRequest, conn);
        cmd.Parameters.Add(_idFile, SqlDbType.Int).Value = idFile;
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        string serverPath = rdr.GetSqlString(0).Value;
        byte[] serverTxn = rdr.GetSqlBinary(1).Value;
        rdr.Close();
        return new SqlFileStream(serverPath, serverTxn, FileAccess.Read);

But I get an exception at rdr.GetSqlBinary(1).Value because GET_FILESTREAM_TRANSACTION_CONTEXT return null. I found here that this is due the missing transaction.

I tried with a “TransactionScope”+its .Complete(); call. Doesn’t change anything.

I tried to do a BEGIN TRANSACTION like showed in the previous link:

        SqlConnection connection = GetConnection();
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "BEGIN TRANSACTION";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.ExecuteNonQuery();

        cmd = new SqlCommand(_selectMetaDataRequest, connection);
        cmd.Parameters.Add(_idFile, SqlDbType.Int).Value = idFile;
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        string serverPath = rdr.GetSqlString(0).Value;
        byte[] serverTxn = rdr.GetSqlBinary(1).Value;
        rdr.Close();
        SqlFileStream sqlFileStream = new SqlFileStream(serverPath, serverTxn, FileAccess.Read);
      cmd = new SqlCommand();
        cmd.CommandText = "COMMIT TRANSACTION";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        cmd.ExecuteNonQuery();

But it crashes on the first “ExecuteNonQuery” with the exception "A transaction that was started in a MARS batch is still active at the end of the batch. The transaction is rolled back." But it’s the FIRST query executed!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:46:05+00:00Added an answer on May 28, 2026 at 5:46 pm

    Let’s have an example. We could start by defining a contract that will describe the operation we are willing to perform:

    public interface IPhotosRepository
    {
        void GetPhoto(int photoId, Stream output);
    }
    

    We will see the implementation later.

    Now we could define a custom action result:

    public class PhotoResult : FileResult
    {
        private readonly Action<int, Stream> _fetchPhoto;
        private readonly int _photoId;
        public PhotoResult(int photoId, Action<int, Stream> fetchPhoto, string contentType): base(contentType)
        {
            _photoId = photoId;
            _fetchPhoto = fetchPhoto;
        }
    
        protected override void WriteFile(HttpResponseBase response)
        {
            _fetchPhoto(_photoId, response.OutputStream);
        }
    }
    

    then a controller which will allow us to show the photo:

    public class HomeController : Controller
    {
        private readonly IPhotosRepository _repository;
    
        public HomeController(IPhotosRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult Photo(int photoId)
        {
            return new PhotoResult(photoId, _repository.GetPhoto, "image/jpg");
        }
    }
    

    and a corresponding view in which we are going to show the photo in an <img> tag using the Photo action:

    <img src="@Url.Action("photo", new { photoid = 123 })" alt="" />
    

    Now the last part is of course the implementation of the repository which might look something along the lines of:

    public class PhotosRepositorySql : IPhotosRepository
    {
        private readonly string _connectionString;
        public PhotosRepositorySql(string connectionString)
        {
            _connectionString = connectionString;
        }
    
        public void GetPhoto(int photoId, Stream output)
        {
            using (var ts = new TransactionScope())
            using (var conn = new SqlConnection(_connectionString))
            using (var cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText =
                @"
                    SELECT 
                        Photo.PathName() as path, 
                        GET_FILESTREAM_TRANSACTION_CONTEXT() as txnToken 
                    FROM 
                        PhotoAlbum 
                    WHERE 
                        PhotoId = @PhotoId
                ";
                cmd.Parameters.AddWithValue("@PhotoId", photoId);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        var path = reader.GetString(reader.GetOrdinal("path"));
                        var txnToken = reader.GetSqlBinary(reader.GetOrdinal("txnToken")).Value;
                        using (var stream = new SqlFileStream(path, txnToken, FileAccess.Read))
                        {
                            stream.CopyTo(output);
                        }
                    }
                }
                ts.Complete();
            }
        }    
    }
    

    All that’s left now is to instruct your favorite DI framework to use PhotosRepositorySql.

    This technique allows you to efficiently work with arbitrary big files as it never loads the entire stream into memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an ASP.Net MVC project that will start with a single web server
I've a ASP.NET MVC 2 project that runs fine under Windows 7 and will
I'm starting a project that will be public facing using asp.net mvc. I know
I've made an Area project for my ASP.NET MVC application called 'Admin'. This will
I have to develop a fairly large ASP.NET MVC project very quickly and I
I am currently starting a new ASP.NET MVC Project at work where we need
We are starting a new ASP.NET MVC project. The web application will be used
I am using an ASP.NET MVC project and everytime I add a class to
I have an ASP.NET MVC project with a form. In the Action method that
I have an ASP.NET MVC project and I have a single action that accepts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.