Wednesday, 17 June 2015

DownLoad file using C#

private void Download(string filePath, string fileName)
        {
            //string fileExtension = ".txt";
            // Set Response.ContentType
            Response.ContentType = msdnh.util.DownloadFile.GetContentType(fileName);

            // Append header
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);

            // Write the file to the Response
            Response.TransmitFile(Server.MapPath(filePath + Path.GetFileName(fileName)));
            Response.End();

        }

public static class DownloadFile
    {
        public static string GetContentType(string fName)
        {
            if (string.IsNullOrEmpty(fName))
                return string.Empty;

            string contentType = string.Empty;
            if (fName.EndsWith(".txt"))
            {
                contentType = "application/txt";
            }
            else if (fName.EndsWith(".pdf"))
            {
                contentType = "application/pdf";
            }
            else if (fName.EndsWith(".doc"))
            {
                contentType = "application/msword";
            }
            else if (fName.EndsWith(".docx"))
            {
                contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            }
            else if (fName.EndsWith(".xls"))
            {
                contentType = "application/vnd.ms-excel";
            }
            else if (fName.EndsWith(".xlsx"))
            {
                contentType = "application/vnd.xlsx";
            }
            else if (fName.EndsWith(".jpg"))
            {
                contentType = "image/jpg";
            }
            else if (fName.EndsWith(".gif"))
            {
                contentType = "image/GIF"; ;
            }
            else
            {
                contentType = "application/octet-stream";
            }

            return contentType;
            //switch (fileExtension)
            //{
            //    case ".htm":
            //    case ".html":
            //        contentType = "text/HTML";
            //        break;

            //    case ".txt":
            //        contentType = "text/plain";
            //        break;

            //    case ".doc":
            //    case ".rtf":
            //    case ".docx":
            //        contentType = "Application/msword";
            //        break;

            //    case ".xls":
            //    case ".xlsx":
            //        contentType = "Application/x-msexcel";
            //        break;

            //    case ".jpg":
            //    case ".jpeg":
            //        contentType = "image/jpeg";
            //        break;

            //    case ".gif":
            //        contentType = "image/GIF";
            //        break;

            //    case ".pdf":
            //        contentType = "application/pdf";
            //        break;
            //}

           
        }
    }