File download does not work

Asked

Viewed 1,028 times

2

How to download a file via browser from a directory in my project?

I tried it this way, but nothing happens:

 string path = "C:\\test.txt";
        var file = new FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "text/plain";
            Response.Flush();
            Response.TransmitFile(file.FullName);
            Response.End();
        }

I debugged and the values are all right, but do not download anything in the browser, as I do?

<system.web>
<!-- Autenticação-->
<authentication mode="Forms">
  <forms name="login" loginUrl="login.aspx" protection="All" timeout="1" defaultUrl="pagina.aspx" requireSSL="false"/>
</authentication>
<compilation debug="true" targetFramework="4.5">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  </assemblies>
</compilation>
  <httpRuntime maxRequestLength="50000" />
<pages controlRenderingCompatibilityVersion="4.0"/>

  • You’re using MVC by chance?

  • My mvc is manual, I do not use the standard of microsoft Asp.net mvc, is web Forms

  • I tested in another project my code from the beginning and it works, which can be?

  • @Warlock this code is triggered by a control inside an updatepanel?

3 answers

0

I tested your code here and it works normally.

Try to change the contenttype like this:

//Response.ContentType = "text/plain";
Response.ContentType = "application/force-download";
Response.AddHeader("Content-Transfer-Encoding", "binary");
  • put and nothing.

  • he’s coming in, as I mentioned in the post I debugged.

  • No download in browser yet

  • Since you said it worked there, I changed the title to see if someone finds out why it doesn’t work.

  • I haven’t been able to

  • I tested in another project my code from the beginning and it works, which can be?

  • Try to compare the files web.config especially in the section <system.webServer>.

  • there is no system.webserver, but system.web

  • posted the web config

  • I tested it on pageload and it worked

  • but my event doesn’t work

Show 6 more comments

0

You’re having some problems with the code. Although I haven’t tried it, have you tried it? I adapted the idea from here.

    Response.Clear();
    Response.ClearHeaders();
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "text/plain";
    System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path));
    Response.TransmitFile(Dfile.FullName);
    HttpContext.Current.ApplicationInstance.CompleteRequest();
  • line error: System.IO. says that the directory is physical and expects a virtual one

  • Just put System.IO.FileInfo Dfile = new System.IO.FileInfo(path);

  • nothing yet, it is strange, no error appears or anything. no debug passes all the steps. but no download appears in the browser

  • Try Response.WriteFile(path); instead of Response.TransmitFile(Dfile.FullName);

  • hasn’t been yet...

  • I tested in another project my code from the beginning and it works, which can be?

Show 1 more comment

0


@Warlock, with the knowledge I have about its application (adquerido of other questions from you), I believe that its problem is not in the download itself, but in the mechanics of the UpdatePanel.

Then I advise you to modify or add the event ItemDataBound to his Repeater, then register Control for Postback.

protected void <repeaterID>_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var btDownload = e.Item.FindControl("<buttonID>") as Button;
        this.<scriptManagerID>.RegisterPostBackControl(btDownload);
    }
}
  • that’s exactly what you said, when I take the button off the updatepanel, it works, but this way I could not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.