恢复 ASP.NET 响应标头

发布于 2024-09-06 14:56:44 字数 3126 浏览 2 评论 0原文

以编程方式创建了一个列表视图,用于显示图像。当您单击下载时,将触发 ItemCommand,浏览器将图像作为二进制响应发送给用户,使用以下命令:

SPFile ImageIfile = spfolder.Files[ServerName];
byte[] bs = ImageIfile.OpenBinary();
string res = Page.Response.ContentType;
Page.Response.ContentType = "image/jpeg";
Page.Response.AppendHeader("Content-Disposition", "attachment; filename=" +  Path.GetFileName(fileName))
Page.Response.BinaryWrite(bs);
Page.Response.End();

这有效,仅一次。然后既不是下载链接,也不是DataPage分页控件 一直工作直到刷新(实际上是任何回发)。

编辑:这是一个 SharePoint 2007 WebPart,这是 CreateChildControls 方法中 ListView 的声明:

lv.ItemPlaceholderID = "itemPlaceholder";
lv.GroupPlaceholderID = "groupPlaceholder";
lv.ID = "MediaSearch";
lv.LayoutTemplate = new LayoutTemplate(); 
lv.GroupTemplate = new GroupTemplate(); 
lv.GroupItemCount = 4;
lv.ItemTemplate = new ItemTemplate(); 
lv.EmptyDataTemplate = this.Page.LoadTemplate("/usercontrols/MediaResults_Empty.ascx");

这是 ItemTemplate 和 DataBinding

public class ItemTemplate : ITemplate
{
   public void InstantiateIn(Control container)
   {
       //Top bit
       Panel ItemPanel = new Panel();
       ItemPanel.ID = "itemPlaceholder";
       ItemPanel.Attributes["class"] = "mlitem";
       var thumbdiv = new HtmlGenericControl("div");
       thumbdiv.Attributes["class"] = "thumb-image";
       HyperLink aspLink = new HyperLink();
       aspLink.ID = "hlPicPreview";
       aspLink.Attributes["class"]="picture-preview";
       Image aspImg = new Image();
       aspImg.ID = "thumb";

       aspLink.Controls.Add(aspImg);
       thumbdiv.Controls.Add(aspLink);
       ItemPanel.Controls.Add(thumbdiv);

       //Bottom bit
       var bDiv = new HtmlGenericControl("div");
       bDiv.Attributes["class"] = "details";
       var UnOrderedList = new HtmlGenericControl("ul");
       var li1 = new HtmlGenericControl("li");
       Literal lit = new Literal();
       lit.ID = "liSize";
       lit.Text = "Size";
       li1.Controls.Add(lit);
       var li2 = new HtmlGenericControl("li");
       LinkButton down = new LinkButton();
       down.ID = "lbDownload";
       down.CommandArgument = "Pugs";
       down.CommandName = "Download";
       down.Text = "Download";
       li2.Controls.Add(down);
       UnOrderedList.Controls.Add(li1);
       UnOrderedList.Controls.Add(li2);
       bDiv.Controls.Add(UnOrderedList);

       ItemPanel.Controls.Add(bDiv);
       ItemPanel.DataBinding += new EventHandler(ItemPanel_DataBinding);
       container.Controls.Add(ItemPanel);
   }

   void ItemPanel_DataBinding(object sender, EventArgs e)
   {
       Panel ThePanel = (Panel)sender;
       //Get bindables
       Image thumb = ThePanel.FindControl("thumb") as Image;
       LinkButton lbdown = ThePanel.FindControl("lbDownload") as LinkButton;
       ListViewDataItem lvdi = (ListViewDataItem)ThePanel.NamingContainer;

       //Bind that stuff.
       lbdown.CommandArgument = ((DataRowView)lvdi.DataItem)["URL"].ToString();
       thumb.ImageUrl = "~/" + ((DataRowView)lvdi.DataItem)["ThumbsNailsImg"].ToString();
   }

我在这里有点困惑。站点上启用了 AJAX,但在此控件中未使用 AJAX。

've programmatically created a listview, for displaying images. When you click on the download the ItemCommand is fired, and the browser sends the user the image as a binary response, using the following:

SPFile ImageIfile = spfolder.Files[ServerName];
byte[] bs = ImageIfile.OpenBinary();
string res = Page.Response.ContentType;
Page.Response.ContentType = "image/jpeg";
Page.Response.AppendHeader("Content-Disposition", "attachment; filename=" +  Path.GetFileName(fileName))
Page.Response.BinaryWrite(bs);
Page.Response.End();

This works, exactly once. Then neither the download link, nor the DataPage paging controls
work until you refresh (Indeed any postbacks).

EDIT: It's a SharePoint 2007 WebPart, this is the declaration of the ListView in the CreateChildControls method:

lv.ItemPlaceholderID = "itemPlaceholder";
lv.GroupPlaceholderID = "groupPlaceholder";
lv.ID = "MediaSearch";
lv.LayoutTemplate = new LayoutTemplate(); 
lv.GroupTemplate = new GroupTemplate(); 
lv.GroupItemCount = 4;
lv.ItemTemplate = new ItemTemplate(); 
lv.EmptyDataTemplate = this.Page.LoadTemplate("/usercontrols/MediaResults_Empty.ascx");

And this is the ItemTemplate and DataBinding

public class ItemTemplate : ITemplate
{
   public void InstantiateIn(Control container)
   {
       //Top bit
       Panel ItemPanel = new Panel();
       ItemPanel.ID = "itemPlaceholder";
       ItemPanel.Attributes["class"] = "mlitem";
       var thumbdiv = new HtmlGenericControl("div");
       thumbdiv.Attributes["class"] = "thumb-image";
       HyperLink aspLink = new HyperLink();
       aspLink.ID = "hlPicPreview";
       aspLink.Attributes["class"]="picture-preview";
       Image aspImg = new Image();
       aspImg.ID = "thumb";

       aspLink.Controls.Add(aspImg);
       thumbdiv.Controls.Add(aspLink);
       ItemPanel.Controls.Add(thumbdiv);

       //Bottom bit
       var bDiv = new HtmlGenericControl("div");
       bDiv.Attributes["class"] = "details";
       var UnOrderedList = new HtmlGenericControl("ul");
       var li1 = new HtmlGenericControl("li");
       Literal lit = new Literal();
       lit.ID = "liSize";
       lit.Text = "Size";
       li1.Controls.Add(lit);
       var li2 = new HtmlGenericControl("li");
       LinkButton down = new LinkButton();
       down.ID = "lbDownload";
       down.CommandArgument = "Pugs";
       down.CommandName = "Download";
       down.Text = "Download";
       li2.Controls.Add(down);
       UnOrderedList.Controls.Add(li1);
       UnOrderedList.Controls.Add(li2);
       bDiv.Controls.Add(UnOrderedList);

       ItemPanel.Controls.Add(bDiv);
       ItemPanel.DataBinding += new EventHandler(ItemPanel_DataBinding);
       container.Controls.Add(ItemPanel);
   }

   void ItemPanel_DataBinding(object sender, EventArgs e)
   {
       Panel ThePanel = (Panel)sender;
       //Get bindables
       Image thumb = ThePanel.FindControl("thumb") as Image;
       LinkButton lbdown = ThePanel.FindControl("lbDownload") as LinkButton;
       ListViewDataItem lvdi = (ListViewDataItem)ThePanel.NamingContainer;

       //Bind that stuff.
       lbdown.CommandArgument = ((DataRowView)lvdi.DataItem)["URL"].ToString();
       thumb.ImageUrl = "~/" + ((DataRowView)lvdi.DataItem)["ThumbsNailsImg"].ToString();
   }

I more than a little stumped here. AJAX is enabled on the site, but not being used in this control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-09-13 14:56:44

您遇到的情况是 SharePoint 试图变得聪明。
当网站速度变慢时(即使在运行 SharePoint 时也可能发生这种情况),用户往往会多次按下按钮/链接,这不会使事情变得更快,并且可能会导致麻烦,因为事件被触发两次。因此 SharePoint 默认情况下禁用多个回发。

几乎所有有关将 AJAX 与 SharePoint 结合使用的文章中都可以找到此问题的修复方法。查找您应该在显示列表视图的代码中实现和调用的 EnsurePanelFix 函数。

What you're running into is SharePoint trying to be clever.
When a Web site it slow (and this can happen even when running SharePoint) users tend to push buttons/links multiple times, which isn't going to make things faster and may cause trouble as events are fired twice. So SharePoint is by default disabling multiple postbacks.

The fix for this can be found in almost any article about using AJAX with SharePoint. Look for the EnsurePanelFix function which you should implement and call in the code showing the listview.

荆棘i 2024-09-13 14:56:44

这个问题与列表视图甚至SharePoint(一次)无关。
我使用的是 itemCommand 方法,它是一个回发,因此当处理回发时,我的代码突然劫持 HTTP 响应,然后将输出更改为图像类型并添加附件标头。这意味着 ASP.NET 无法再像平常那样处理流。
解决方案:
我将下载链接设为标准锚标记,指向解决该问题的 httpHandler。

The problem was nothing to do with the listview or even SharePoint(for once).
I was using the itemCommand method, which is a postback, so when the postback is being processed my code suddenly hijacks the HTTP response then changes the output to a image type and adds an attachment header. This means that the asp.net can no longer process the stream as it would usually.
solution:
I made the download link a standard anchor tag, pointing to a httpHandler that solves that issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文