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?
– Richard Dias
My mvc is manual, I do not use the standard of microsoft Asp.net mvc, is web Forms
– War Lock
I tested in another project my code from the beginning and it works, which can be?
– War Lock
@Warlock this code is triggered by a control inside an updatepanel?
– Marciano.Andrade