开发者

C# downloading multiples files from the server side through the browser

Anyone can help me on downloading multiple files from the server side through the browser (i.e. Chrome) I've debbuged it the foreach iterates fine but it only downloads the first file. Any suggestions would be greatly appreciated cheers since I'm getting way frustrated.

Here's the code snippet:

FileInfo fInfo;
    SQLConnection = new SqlConnection(SQLConnectionString);
    foreach (CartItem CartProduct in Cart.Instance.Items)
    {
        SQLCom = new SqlCommand("spSelect_T", SQLConnection);
        SQLCom.CommandType = CommandType.StoredProcedure;
        SQLCom.Parameters.Add(new SqlParameter("@id", CartProduct.ProductId));        
        SQLConnection.Open();
        SQLDReader = SQLCom.ExecuteReader();
        if (SQLDReader.Read() == true)
        {
            fInfo = new FileInfo(Server.MapPath(SQLDReader["t_path"].ToString()));
            Response.Clear();
            Response.Expires = -1;
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fInfo.Name + "\"");
            Response.AddH开发者_JAVA百科eader("Content-Length", fInfo.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(fInfo.FullName);
            Response.End();
        }
        SQLConnection.Close();
    }


Each request can only have one response. I recommend you zip up the files and send them back in one package.


Scriptworks, you're fighting against the way the protocol works. Doesn't matter whether you use ASP/PHP, or any other web framework. You can only send one set of headers and a body stream back for any given request. Your options are limited.

With respect to files this means you can transfer only one file per HTTP request. If other suggestions like Zipping to create a single response file don't work, your only alternative is to get the client to make multiple requests.

Easiest is probably doing this in AJAX, or hidden IFRAMEs.

First you're going to need to either have a handler that takes a filename and returns it's headers and filestream, or a static file web server with the files in it's content directory.

In the case of AJAX, you'd send a client-script the list of file URLs, so it can in script iterate through the list, requesting each URL in turn.

The latter, using server or script to generate IFRAMEs whose src attribute is set to the URLs of your files.

Neither of these solutions are "pretty" user experiences.


I would guess because you are calling Response.End() inside the loop. After the first iteration it prevents anything else going back to the client.

Why don't you zip your files together into a single download? There are good, open source C# compression libraries that make this simple.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜