开发者

how to use save dialog box for download files

in my project i have a folder containing N number of files. I need a functionality that the page should have links for all those files so that client can download those files.

For this i have dynamically created Link Buttons and linked each file with each link Button.

The Code is as

 string filePath = Request.PhysicalPath.Substring(0, Request.PhysicalPath.LastIndexOf("\\"));
        filePath = filePath.Substring(0, filePath.LastIndexOf("\\"));
        filePath = filePath.Substring(0, filePath.LastIndexOf("\\")) + "\\Export";

        string[] files = System.IO.Directory.GetFiles(filePath);
        Array.Sort(files);
        Array.Reverse(files);
        int counter = 0;
        foreach (string file in files)
        {
            if (counter >= 10)
                break;
            LinkButton linkButton = new LinkButton();
            int startIndex = Convert.ToInt32(file.LastIndexOf("\\"));
            int length = Convert.ToInt32(file.Length);
            string fileName = file.Substring(startIndex + 1, length - startIndex - 1).ToString();
            fileName = fileName.Substring(0, fileName.LastIndexOf("."));
            string[] a = fileName.Split('_');
            string year = a[1].Substring(0, 4);
            string month = a[1].Substring(4, 2);
            switch (month)
            {
                case "01":
                    month = "January";
                    break;
                case "02":
                    month = "February";
                    break;

                case "03":
                    month = "March";
                    break;
                case "04":
                    month = "April";
                    break;
                case "05":
                    month = "May";
                    break;
                case "06":
                    month = "June";
                    break;
                case "07":
                    month = "July";
                    break;
                case "08":
                    month = "August";
                    break;
                case "09":
                    month = "September";
                    break;
                case "10":
                    month = "October";
                    break;
                case "11":
                    month = "November";
                    break;
                case "12":
                    month = "December";
                    break;

            }
            counter++;
            linkButton.ForeColor = System.Drawing.ColorTranslator.FromHtml("#b32317");
            linkButton.Text = month + " " + year + " Car Hire Details";
            //hyperlink.NavigateUrl = file.ToString();
            linkButton.Font.Bold = true;
            //hyperlink.ID = "TestLB";
            linkButton.Command += new CommandEventHandler(this.lb_Command);
            linkButton.CommandArgument = file.ToString(开发者_JAVA技巧);
            Panel1.Controls.Add(linkButton);
            Label lbl1 = new Label();
            lbl1.Text = "<br /> <br />";
            Panel1.Controls.Add(lbl1);

        }


    }


    protected void lb_Command(object sender, CommandEventArgs e)
    {

        System.String filename = e.CommandArgument.ToString();// this is your file name 
        Response.ContentType = "Plain/Text";
        System.String disHeader = "Attachment; Filename=\"" + filename + "\"";
        Response.AppendHeader("Content-Disposition", disHeader);
        System.IO.FileInfo fileToDownload = new System.IO.FileInfo(e.CommandArgument.ToString());
        //filepath to download 
        Response.Flush();
        Response.WriteFile(fileToDownload.FullName);


    }

But the problem is that, when i am trying to download this:

  1. the file is downloading in HTML format, moreover the file name of downloaded file becomes same to my Code file name where as i want the actual name.

  2. the downloaded file have the actual data but it also contains the html code to the page on which this links are coming

Can any body help me out for this.


Response.ContentType = "APPLICATION/OCTET-STREAM";

Thats your problem

Look up the kind of file you are allowing for download here, and modify it appropriately.


Simple use the Content-Disposition header as described in RFC 2183. A quick google on the subject brought up this page and it seems quite expansive, so I will not repeat it...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜