首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Textfield not updating with SaveAs对话框

Textfield not updating with SaveAs对话框
EN

Stack Overflow用户
提问于 2012-07-24 21:56:36
回答 1查看 40关注 0票数 0

由于某些原因,当我显示“另存为”对话框时,我的校验和框永远不会更新。但是,如果我注释掉下面的Content-Disposition标头代码,那么一切都会正常工作。有什么想法吗?

代码语言:javascript
复制
protected void SubmitButton_Click(object sender, EventArgs e)
{
    checksumField.Text = String.Empty;

    FileInfo filename = this.CreateFileInfoFromInput();

    if (!filename.Exists)
    {
        tableField.Focus();
        tableField.BorderColor = System.Drawing.Color.Red;
        return;
    }

    this.SaveFile(filename);
    checksumField.Text = GetChecksum(filename);
}

private FileInfo CreateFileInfoFromInput()
{
    /* blah */
    return new FileInfo(blah);
}

private string GetChecksum(FileInfo filename)
{
    return "test";
}

private void SaveFile(FileInfo fileName)
{
    // Clear old headers
    Response.Clear();
    Response.Buffer = false;

    // Set the ContentType and Headers
    Response.ContentType = "application/octet-stream";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName.Name);  //  <<<---- FAILING RIGHT HERE
    Response.AppendHeader("Content-Length", fileName.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
    Response.AppendHeader("Connection", "Keep-Alive"); 

    // Send data
    Response.TransmitFile(fileName.FullName);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-24 22:05:52

您正在缩短页面的生命周期。

您可以在响应中注入文件,而不是让页面自我更新并呈现到响应流

我的建议是遵循以下步骤:

  1. 创建一个简单的处理程序(将根据ID为文件提供服务的ashx文件)
  2. 更改您的页面以生成指向此处理程序(或将打开此url的脚本)的链接

当用户提交一个ID时,像您一样生成文件(希望这个过程很短),更新您的文本框(这应该像您做的那样工作),并添加到处理程序文件服务的链接。

只需小心安全隐患即可。不要盲目地发送url中指定的文件(以避免文件路径注入)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11632463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档