下载文件后更改下拉列表索引

时间:2013-07-04 03:42:02

标签: c# asp.net

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

当用户选择下载选项时,我使用Response来下载文件。下载文件后,我想将下拉列表索引更改为0.我通过在下载文件后将所选索引下拉列表设置为0来尝试它。但它根本不是畏缩。 如果在下载文件后发生任何回发,则再次在下拉列表中自动选择下载选项。

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

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();
}

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

1 个答案:

答案 0 :(得分:0)

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

只要在下拉列表中选择下载选项,我就会在服务器端调用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)和你可以传递的任何查询字符串,用于下载如 filename,filepath 等。 在处理程序文件的页面加载中,我正在编写用于下载文件的代码。现在,我们可以在下载文件后执行我们想要的操作。