从sql块中下载Varbinary

时间:2019-03-14 14:01:22

标签: c# sql asp.net sql-server

我该如何使用code example below从c#的SQL表中下载?

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();
            }
        }

我无法弄清楚。我不知道如何将Varbinary返回到FileStream。如果文件大于40 MB,我的Varbinary文件下载将失败。我能够获得此代码以从文件服务器下载,但不能用于SQL Varbinary。

以下是我当前正在使用的内容,但是任何大于40 MB的文件都出现网络故障。我试图弄清楚该代码如何以字节块的形式实现下载。

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();
        }

谢谢!

0 个答案:

没有答案