2

How would I go about using the code example below to download from a SQL Table in c#?

public static void DownloadLargeFile(string DownloadFileName, string FilePath, string ContentType, HttpResponse response)
        {
            Stream stream = null;

            // read buffer in 1 MB chunks
            // change this if you want a different buffer size
            int bufferSize = 1048576;

            byte[] buffer = new Byte[bufferSize];

            // buffer read length
            int length;
            // Total length of file
            long lengthToRead;

            try
            {
                // Open the file in read only mode 
                stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                // Total length of file
                lengthToRead = stream.Length;
                response.ContentType = ContentType;
                response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(DownloadFileName, System.Text.Encoding.UTF8));

                while (lengthToRead > 0)
                {
                    // Verify that the client is connected.
                    if (response.IsClientConnected)
                    {
                        // Read the data in buffer
                        length = stream.Read(buffer, 0, bufferSize);

                        // Write the data to output stream.
                        response.OutputStream.Write(buffer, 0, length);

                        // Flush the data 
                        response.Flush();

                        //buffer = new Byte[10000];
                        lengthToRead = lengthToRead - length;
                    }
                    else
                    {
                        // if user disconnects stop the loop
                        lengthToRead = -1;
                    }
                }
            }
            catch (Exception exp)
            {
                // handle exception
                response.ContentType = "text/html";
                response.Write("Error : " + exp.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                response.End();
                response.Close();
            }
        }

I am unable to figure this out. I can't figure out how I would get the Varbinary back to the FileStream. My Varbinary file download fails if the file is larger than 40 MB. I am able to get this code to work for downloading from the file server, but not for the SQL Varbinary.

Below is what I am currently using, but I get Network-Failure on any file larger than 40 MB. I am trying to figure out how I would implement downloading in byte chunks for this code.

protected void lnkDownloadIR_Click(object sender, EventArgs e)
        {

            LinkButton lnkbtn = sender as LinkButton;
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            int id = Convert.ToInt32(gvrow.Cells[2].Text);
            byte[] bytes;
            string fileName, contentType;
            Int64 fileSizeInBytes;
            string constr = ConfigurationManager.ConnectionStrings["OCMCA_CEConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT FileName, FileData, ContentType FROM InstructorResources where Id=@Id";
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                        bytes = (byte[])sdr["FileData"];
                        contentType = sdr["ContentType"].ToString();
                        fileName = sdr["FileName"].ToString();

                    }
                    con.Close();
                }
            }
            Response.Clear();
            Response.Buffer = true;
            Response.BufferOutput = false;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = contentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            fileSizeInBytes = bytes.Length;
            Response.AddHeader("Content-Length", fileSizeInBytes.ToString());
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

Thanks!

Oleksiakd
  • 43
  • 3
  • 1
    SQL Server? [Streaming VARBINARY data from SQL Server in C#](https://stackoverflow.com/questions/5042589/streaming-varbinary-data-from-sql-server-in-c-sharp) – Alex K. Mar 14 '19 at 14:04
  • @AlexK. I edited my question a bit. I am not sure if FileStream is the correct route. Thanks for any help, I am new to this. – Oleksiakd Mar 14 '19 at 15:32

0 Answers0