Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In an application i have written this code for downloading a file:
C#
try
            {
                string strURL = " Mandatory Disclosure-20101.doc";
                WebClient req = new WebClient();
                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.ClearContent();
                response.ClearHeaders();
                response.Buffer = true;
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
                byte[] data = req.DownloadData(Server.MapPath(strURL));
                response.BinaryWrite(data);
                response.End();
            }
            catch (Exception ex)
            {
            }

This code working properly but it is downloading the file directly.
I want to prompt a dialogue box which asks to save or to open the file?
Posted

1 solution

Try this method .i think this will help you

C#
private void DownloadFiles()
    {
        string filePath = "";
        if (Convert.ToString(Request.QueryString["module"]) == "INV")
            filePath = Server.MapPath("~") + Convert.ToString(ConfigurationSettings.AppSettings["Upload"]) + Convert.ToString(Request.QueryString["fileName"]); //@"E:\NewFolder\MRP_20100311.xls";
        try
        {
            if (File.Exists(filePath))
            {
                FileInfo objFileInfo = new FileInfo(filePath);
                WebClient client = new WebClient();
                Byte[] buffer = client.DownloadData(filePath);
                Response.Clear();
                Response.ContentType = GetContentType(objFileInfo.Extension); //"application/ms-excel";
                Response.AddHeader("content-disposition", "Attachment;filename=" + objFileInfo.Name); //content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
                Response.End();
            }
        }
    }
private string GetContentType(string strExt)
    {
        //strExt = strExt.ToUpper();
        string type = "";
        if (strExt != null)
        {
            switch (strExt.ToLower())
            {
                case ".htm":
                case ".html":
                    type = "text/HTML";
                    break;
                case ".txt":
                    type = "text/plain";
                    break;
                case ".xml":
                    type = "text/xml";
                    break;
                case ".doc":
                case ".docx":
                case ".rtf":
                    type = "Application/msword";
                    break;
                case ".xls":
                case ".xlsx":
                case ".csv":
                    type = "Application/ms-excel";
                    break;
            }
        }

        if (type == "")
            return "APPLICATION/OCTET-STREAM";
        return type;
    }
 
Share this answer
 
Comments
Qureshali 27-Jan-12 7:23am    
This code working nut my problem has not been solved yet sir. This code also directly downloads the file without any prompt. Please suggest any other idea.
Qureshali 28-Jan-12 0:18am    
Sir, please do reply with my query please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900