下载文件后更改下拉索引

本文关键字:索引 文件 下载 | 更新日期: 2023-09-27 18:20:52

我有一个下拉列表,其中有下载作为一个选项

当任何用户选择下载选项时,我将使用响应来下载文件。下载文件后,我想将下拉列表索引更改为0。下载文件后,我尝试将下拉列表的选定索引设置为0。但这一点也不令人反感。如果在下载文件后发生任何回发,请再次Download下拉列表中自动选择的选项。

选择下载选项时,我正在调用以下方法。

protected void downloadfile()
{
         Response.Clear();
         Response.Buffer = false;
         Response.AddHeader("Accept-Ranges", "bytes");
         Response.ContentType = "application/octet-stream";
         Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
         Response.AddHeader("Connection", "Keep-Alive");
         Response.OutputStream.Write(buffer, 0, bytesRead);
         Response.Flush();
}

请分享您解决此问题的建议。。。。

下载文件后更改下拉索引

我使用Iframe解决了这个问题。

一旦在下拉菜单中选择了Download选项,我将在服务器端调用javascript函数DownloadFile(),如下所示:

protected void ddlSelectedAction_SelectedIndexChanged(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.Page, GetType(), "download", "DownloadFile();", true);
    // We can do what ever we want, You can redirect to another page or anything you want
    //Here I am changing the ddlSelectedAction selected index to 0
    ddlSelectedAction.SelectedIndex = 0;
}

下面是javascript函数

DownloadFile()
{
    var filename= $get("Filename").value;
    // Point the IFRAME to GenerateFile, with the
    //   desired region as a querystring argument.
    iframe.src = "GenerateFile.ashx?fname=" + filename;
    // This makes the IFRAME invisible to the user.
    iframe.style.display = "none";
    // Add the IFRAME to the page.  This will trigger
    //   a request to GenerateFile now.
    document.body.appendChild(iframe); 
}   

在上面我正在创建Iframe的函数中,源将是handlerfile(GenerateFile.ashx)和您可以传递的任何查询字符串,这些字符串用于下载,如文件名、文件路径等。在处理程序文件的页面加载中,我正在编写下载该文件的代码。现在我们可以在下载文件后进行任何操作。