How to fix page freeze after downloading a file? (TransmitFile in TreeView SelectedNodeChanged)

So, I know that this is a common problem, the page freezes (any buttons are visually clickable, but do not perform any actions), because they do not close any request after downloading the file.

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/txt";
response.AddHeader("Content-Disposition", "attachment; filename=" + TreeView1.SelectedNode.Text + ";");
response.TransmitFile(TreeView1.SelectedNode.Value);
response.Flush();
response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();

This code is in the TreeView event SelectedNodeChanged, and that is the problem.

For example, if you put this transfer code in some button, I can fix the page freeze by adding OnClientClick="javascript:setFormSubmitToFalse()"to the button, and this small JavaScript patch page is fixed after loading.

<script type="text/javascript">
    //Fix page freeze after download dialog
    function setFormSubmitToFalse() {
        setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
        return true;
    }
</script>

, , TreeView node. TreeView OnClientClick, JS , Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "setFormSubmitToFalse()", true); , , .

, ? .

- ( asp.net) sharepoint.

Edit:

, .

, : , , , , , .

SQL, . - treeview SelectedNodeChanged,

SPSecurity.RunWithElevatedPrivileges(delegate()
   {
   /* transmit file code */
   }); 

, , - , , , TransmitFile , JS , OnClientClick.

, , , treeview qaru.site/questions/126019/...

+4
2

, , .

onClientClick, JS click , ? , , TreeView node Span:

...
TreeNode fileNode = new TreeNode
 {
     //before
     //Text = file.Name,
     //after
     Text = "<span onclick=\"javascript:setFormSubmitToFalse();\">"+file.Name+"</span>",
     Value = file.FullName,
 };
...

node :

response.AddHeader("Content-Disposition", "attachment; filename=" + 
    TreeView1.SelectedNode.Text.Replace("<span onclick=\"javascript:setFormSubmitToFalse();\">", string.Empty).Replace("</span>", string.Empty) + ";");

, .

+4

, , , , - , , .

, .

public async void OnSelectedNodeChanged(object sender, EventArgs e)
{
    await Task.Run(() => {
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "text/txt";
        response.AddHeader("Content-Disposition", "attachment; filename=" + TreeView1.SelectedNode.Text + ";");
        response.TransmitFile(TreeView1.SelectedNode.Value);
        response.Flush();
        response.Close();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }).ConfigureAwait(false);
}

async void, , , , , , .

, " " , , .

, , , - , , .

+2

Source: https://habr.com/ru/post/1658904/


All Articles