2

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..

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

1 Comment

ya i have seted as u mention but problem not solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.