返回多个文件以从asp.net下载 [英] Return multiple files to download from asp.net

查看:166
本文介绍了返回多个文件以从asp.net下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁知道该怎么做?我有一个循环,在该循环中建立了一个列表.我希望将每个列表另存为具有不同文件名(duh)的不同文件.我为此使用HttpContext.Current.Response.单个文件可以正常工作,但是,我无法循环播放.我在第二次迭代中收到的错误是服务器在发送HTTP标头后无法清除标头".我的代码如下所示.//重要的当然是//回应用户的评论部分.任何评论高度赞赏!

Who know how to do this? I have a loop and in that loop a List is build up. I want each List to be saved as a different file with a different filename (duh). I am using HttpContext.Current.Response for this. A single file works fine, however, I cannot loop it. The error I receive upon the second iteration is "Server cannot clear headers after HTTP headers have been sent." My code is somewhat like below. What matters of course is the part commented by // response to user. Any comments highly appreciated!

Cheerz, 罗纳德(Ronald)

Cheerz, Ronald

        foreach (GridViewRow dr in GridView1.Rows)
        {
            // find the check box in the row
            System.Web.UI.WebControls.CheckBox cb = (System.Web.UI.WebControls.CheckBox)dr.FindControl("chkbx_selected");

            // see if it's checked
            if (cb != null && cb.Checked)
            {
                // get the cell
                DataControlFieldCell Cell = GetCellByName(dr, "GUID");
                string guid = new Guid(Cell.Text);

                // get the record filtered on the GUID
                var result_json = call_WEBAPI(guid);

                // result_json contains serialized List<string>
                List<string> result = JsonConvert.DeserializeObject<List<string>>(result_json);

                // response to user
                string filename = result[0] + ".txt";
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", filename));
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                foreach (string line in result)
                {
                    HttpContext.Current.Response.Write(line + Environment.NewLine);
                }

                HttpContext.Current.Response.Flush();

            }
        }

        HttpContext.Current.Response.End();

更新:现在,我在内存中创建一个zip存档,创建一个条目并用writer填充它.但是,如何在.zip中创建第二个条目?下面的代码不起作用,错误是在创建模式下的条目只能写入一次,并且一次只能打开一个条目."

Update: I now create a zip archive in memory, create an entry and fill this with a writer. But how do I create the second entry in the .zip?? The code below does not work, error is "Entries in create mode may only be written to once, and only one entry may be held open at a time."

        using (MemoryStream zipToOpen = new MemoryStream())
        {
            using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create, true))
            {
                ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                {
                    writer.WriteLine("Information about this package.");
                    writer.WriteLine("========================");
                }

                List<string> result = new List<string>();

                result.Add("line1");
                result.Add("line2");

                ZipArchiveEntry readmeEntry2 = archive.CreateEntry("Readme2.txt");
                using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                {
                    writer.Write(result);
                }

            }

            using (var fileStream = new FileStream(@"C:\test.zip", FileMode.Create))
            {
                zipToOpen.Seek(0, SeekOrigin.Begin);
                zipToOpen.CopyTo(fileStream);
            }
        }

推荐答案

单个响应只能包含一个文件". (从技术上讲,HTTP没有文件"的概念,这就是这种情况的原因.HTTP具有标头"和内容".正如错误告诉您的那样,一旦发送标头,您就无法更改它们.因为它们已经发送给客户了.)

A single response can contain only a single "file". (Technically HTTP has no concept of "files", which is kind of why this is the case. HTTP has "headers" and "content". As the error tells you, once the headers are sent you can't change them. Because they've already been sent to the client.)

您将不得不从多个请求/响应中返回文件,或者

You're either going to have to return the files from multiple requests/responses or compress them into a single file and return that.

(另外,为什么要对 text 文件使用"application/octet-stream"?也许对压缩文件使用"application/octet-stream",但是文本文件可能应该是"text/plain"或类似名称.)

(Also, why use "application/octet-stream" for a text file? Use that for the zipped file perhaps, but a text file should probably be "text/plain" or something similar.)

这篇关于返回多个文件以从asp.net下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆