Question

i have application like email messaging system. here i adjust one solution to download all file that are in table from particular post. this is my code:

    protected void lbu_download_all_Click(object sender, EventArgs e)
    {
        if (rpt_file_list.Items.Count > 0)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var query = from f in db.Files
                            where f.Post_History_id == int.Parse(post_id.Value.ToString())
                            select new
                            {
                                FileName = f.File_name,
                                File_ext= f.File_ext
                            };
                foreach (var item in query)
                {
                    System.IO.FileInfo objFile = new FileInfo(Server.MapPath("~/PostFiles/" + item.FileName.ToString() + item.File_ext.ToString()));
                    if (objFile.Exists)
                    {
                        Response.Clear();
                        string strFileName = item.FileName.ToString() + item.File_ext.ToString();
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
                        Response.AddHeader("Content-Length", objFile.Length.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.WriteFile(objFile.FullName);
                        Response.BufferOutput = true; 
                        Response.Flush();
                    }
                }
            }
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type = 'text/javascript'>");
            sb.Append(" No files found to download');");
            sb.Append("</script>");
            ClientScript.RegisterStartupScript(this.GetType(), "script", sb.ToString());
        }
    }

i don't know what is problm please help me..

Was it helpful?

Solution

You won't be able to download multiple files like that, I imagine what is happening is that the loop goes through once and on the second iteration it then throws the exception.

What you really should be doing is zipping all the files into one file to download, this question should give you an idea of what I mean.

By zipping the file you'll also get the benefit of compression (less bandwidth, faster transfer) and the user (in your current scenario) won't be presented with multiple 'Save As' dialog windows (much more professional!).

This link may also help you with some other potential ideas (like having a 'Download' page with URL parameters to identify the file). I'm more a fan of a zipped single file option though!

OTHER TIPS

Do you have Response.BufferOutput = true; set properly? If not, the page will be sent as it is generated, which means the Response.Clear() won't do what you want :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top