.NET Programming With Me

C# : Downloading Files using ASP.NET

Downloading Files From ASP.NET using C#.




protected void DownloadButton_Click(object sender, EventArgs e)
        {
            string fileName = "img1.jpg";
            fileDownload(fileName, Server.MapPath("~/Images/img1.jpg"));
        }


private void fileDownload(string fileName, string fileUrl)
        {
            Page.Response.Clear();
            bool success = ResponseFile(Page.Request, Page.Response, fileName, fileUrl, 1024000);
            if (!success)
                Response.Write("Downloading Error!");
            Page.Response.End();
        }

        public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
        {
            try
            {
                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;

                    int pack = 10240; //10K bytes
                    int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
                    if (_Request.Headers["Range"] != null)
                    {
                        _Response.StatusCode = 206;
                        string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                        startBytes = Convert.ToInt64(range[1]);
                    }
                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                    if (startBytes != 0)
                    {
                        _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                    }
                    _Response.AddHeader("Connection", "Keep-Alive");
                    _Response.ContentType = "application/octet-stream";
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;

                    for (int i = 0; i < maxCount; i++)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(pack));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    return false;
                }
                finally
                {
                    br.Close();
                    myFile.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }


Original article from ASP.NET

ASP.NET: Increasing the fileUpload size

The 4MB default is set in machine.config, but you can override it in you web.config. 
For instance, to expand the upload limit to 20MB, you'd do this:


   


Since the maximum request size limit is there to protect your site, it's best to expand the file-size limit for specific directories rather than your entire application. That's possible since the web.config allows for cascading overrides. 
You can add a web.config file to your folder which just contains the above, or you can use the tag in your main web.config to achieve the same effect:


   
      
   


SQL Server: Aggregate Function in an Update Query

The following code shows us how to use an aggregate function while updating a table.
I've used the sum function.

update t1
set t1.fieldName1 = t2.NewFieldName
from table1 t1
inner join (select fieldName1, sum(fieldName2) as 'NewFieldName' from Table99 
where  group by fieldName1) as t2
on t1.fieldName1 = t2.fieldName1
GO

Example:
update t1
set t1.total_balance = t2.totalPay
from employee t1
inner join (select empId, sum(payment) as totalPay 
from employee_payment
where employee_payment.[year] = 2013
group by empId) as t2
on t1.empId = t2.empId
GO