“在发送HTTP标头后,服务器无法附加标头" [英] "Server cannot append header after http headers have been sent"

查看:209
本文介绍了“在发送HTTP标头后,服务器无法附加标头"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的文件下载代码来提供文件供用户下载-效果很好.
但是,如果我尝试通过调用代码teice来存储2个文件,则会炸毁并显示错误在HTTP标头发送后服务器无法附加标头",它将在创建打开,保存对话框
我试图寻找一些响应属性,这些响应属性会告诉我首次下载尚未完成,但没有看到任何内容.
如何提供多个文件供下载?
这是我的代码:

I have some simple file download code to serve up a file for the user to download - it works fine.
But if I try to server up 2 files by calling the code teice it blows up with the error "Server cannot append header after http headers have been sent" occurabove right where it would create the "open, Save" dialog
I tried to look for some response properties that would tell me the first download was not finished yet but did not see any.
How can I serve up multiple files for download?
Here is my code:

public bool DownloadFile(string targetFileName, string userName, string passWord, Page page1, string t_f)
        {
            try
            {
            string originalfile = page1.Server.MapPath(targetFileName);
            //page1.Response.Write(originalfile);
            //return true;
               FileInfo file=new FileInfo(originalfile);
               long fileSize = file.Length;
                string URI = targetFileName;
               
                    System.IO.Stream responseStream =(Stream) file.OpenRead();
                    int read = 1;
                    page1.Response.Clear();
                    page1.Response.ContentType = "application/octet-stream";
                    page1.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + t_f + "\"");
                    if (fileSize != 0)
                        page1.Response.AddHeader("Content-Length",fileSize.ToString());
             
                    while (read > 0)
                    {
                        byte[] buffer = new byte[1024];
                        read = responseStream.Read(buffer, 0, buffer.Length);
                        page1.Response.OutputStream.Write(buffer, 0, read);
                        page1.Response.Flush();
                    }
                    //page1.Response.Close();
                    //page1.Response.End();
                    responseStream.Close();
                return true;
            }
            catch (Exception e1) { throw new Exception("Download Fails", e1); return false; }
        }


谢谢

推荐答案

好吧,我认为这是不可能的.解决方法是将所有文件压缩为一个zip文件,然后下载.
查看此链接:
http://www.4guysfromrolla.com/articles/092910-1.aspx [ ^ ]

这里的问题是客户端一次只能发出1个请求,并且在逻辑上1个请求应该有1个响应.该请求的标头只能发送一次,并且只能在响应的其他正文数据之前发送,并且一旦发送,客户端浏览器就会根据针对该请求收到的标头开始执行操作.

当您Flush()响应时,它将发送您的标头并将数据添加到输出流.

另外,我不了解客户如何单击一次即可请求多个文件?他的屏幕上必须有一个链接,上面写着全部下载"或类似内容.而且,如果您能够允许多个文件下载,那么当客户在一个文件之后连续不断地获得20-25个文件进行下载/保存/打开时,客户将如何管理?

所以最好的答案是压缩zip中的所有文件,然后将压缩的zip发送到clinet.

谢谢,
Hemant
Well i dont think that is possible. The workaround would be zip all files in a single zip file and then download it.
checkout this link:
http://www.4guysfromrolla.com/articles/092910-1.aspx[^]

The problem here is client can make only 1 request at a time and logically for 1 request there should be 1 response. The header of the request can be sent only once and before every other body data of the response and once it is sent the client browser starts taking action acording to the header received for that request.

when you Flush() the response it sends your header and the data added to the output stream.

Plus i dont understand how will a client can request multiple files in a single click? there must be a link on his screen reads like "Download All" or something similar. And if anyhow you are able to allow multiple file download how will a client manage when he will continously get 20-25 files to download/save/open on after one?

so the best answer is comppress all files in a zip and send the compressed zip to clinet.

Thanks,
Hemant


您只能有一个标题:页面上已经有一个.
请先尝试以下内容:
You can only have one header: you already have one on your page.
Try this first:
page1.Response.Clear();           // Already have this
page1.Response.ClearContent();    // Add this line
page1.Response.ClearHeaders();    // Add this line


这篇关于“在发送HTTP标头后,服务器无法附加标头"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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