【问题标题】:File Download inside iframe在 iframe 中下载文件
【发布时间】:2012-06-14 05:16:47
【问题描述】:

我正在使用以下方法下载文件。在 iframe 内单击按钮。它在除 IE 之外的所有浏览器中都可以正常工作。请有人给我建议一个解决方案

private void DownloadToBrowser(string filePath)
    {
        try
        {
            FileInfo file = new FileInfo(filePath);
            Context.Response.Clear();

            Context.Response.ClearHeaders();

            Context.Response.ClearContent();

            Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

            Context.Response.AddHeader("Content-Length", file.Length.ToString());

            Context.Response.ContentType = "text/plain";

            Context.Response.Flush();

            Context.Response.TransmitFile(file.FullName);

            Context.Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

【问题讨论】:

    标签: asp.net iframe download


    【解决方案1】:

    我建议删除Context.Response.Flush(); 行...我认为没有必要(因为它会作为Context.Response.End(); 行的一部分发生),并且可能会弄乱浏览器在下一行接收文件的方式.

    另外,您传输的文件肯定是纯文本文件吗?如果没有,您需要提供不同的Context.Response.ContentType();

    【讨论】:

      【解决方案2】:

      您很可能错过了 IE 所需的一些 HTTP 响应标头。

      KB on microsoft web

      还可以查看关于 SO 的类似问题:PHP script to download file not working in IE

      【讨论】:

        【解决方案3】:

        你可以试试这样的:

        Context.Response.Buffer = true;
        Context.Response.ContentType = "application/pdf";
        Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName );
        Context.Response.OutputStream.Write(dataBytes ,0,FileContentLength);         
        

        还可以尝试将此添加到您的 aspx 页面:

        <%@ Page aspCompat="True" other attributes %>
        

        【讨论】:

          【解决方案4】:

          为了理解我正在使用数据集, 下载为 Excel 文件

          使用命名空间:

                  using System.IO;
                  using System.Data;   
          
                  DataSet ds = new DataSet("Table");
                  ds.Tables.Add("Table1");
                  ds.Tables[0].Columns.Add("Field1");
                  ds.Tables[0].Columns.Add("Field2");
                  ds.Tables[0].Columns.Add("Field3");
                  ds.Tables[0].Rows.Add();
                  ds.Tables[0].Rows[0][0] = "1";
                  ds.Tables[0].Rows[0][1] = "2";
                  ds.Tables[0].Rows[0][2] = "3";
          
          
                  HttpResponse response = HttpContext.Current.Response;
          
                  // first let's clean up the response.object
                  response.Clear();
                  response.Charset = "";
          
                  // set the response mime type for excel
                  response.ContentType = "application/vnd.ms-excel";
                  response.AddHeader("Content-Disposition", "attachment;filename=sample.xls");
          
                  // create a string writer
                  using (StringWriter sw = new StringWriter())
                  {
                      using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                      {
                          // instantiate a datagrid
                          DataGrid dg = new DataGrid();
                          dg.DataSource = ds.Tables[0];
                          dg.DataBind();
                          dg.RenderControl(htw);
                          response.Write(sw.ToString());
                          response.End();
                      }
                  }
          

          作为 Word 文件下载

          替换

                  response.ContentType = "application/msword";
                  response.AddHeader("Content-Disposition", "attachment;filename=sample.doc");
          

          【讨论】:

            猜你喜欢
            • 2019-06-29
            • 2015-01-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-23
            • 2012-06-20
            • 1970-01-01
            相关资源
            最近更新 更多