Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

So you are running IIS7.5 and you are scripting to download a file to a client but you keep getting then Content-length removed from the headers. So the client won't see how much is left until the down load is done!

 

The trick is this:  Delete the Content-Encoding header or set it to "";

context.Response.AppendHeader("Content-Encoding","");

public void ProcessRequest(HttpContext context)

{

context.Response.BufferOutput = false;

string name = "myfile.mpeg";

string path = Path.Combine(context.Request.PhysicalApplicationPath, name);

FileInfo fileInfo = new FileInfo(path);

long length = new FileInfo(path).Length;

 

context.Response.Clear();

context.Response.ClearHeaders();

context.Response.ClearContent();

context.Response.AppendHeader("Content-Length", length.ToString() );

context.Response.AppendHeader("Content-Encoding","");

context.Response.ContentType = "application/octet-stream";

context.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=\"{0}-{1}\"", length.ToString(),name));

 

 

context.Response.Flush();

byte[] buffer = new byte[6500];

int bytesRead = 0;

using(Stream input = fileInfo.OpenRead())

{

while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)

{

if (context.Response.IsClientConnected)

{

context.Response.OutputStream.Write(buffer, 0, bytesRead);

context.Response.Flush();

}

else

{

bytesRead = -1;

}

}

}

context.ApplicationInstance.CompleteRequest();

return;

}

 

Keywords:

Content-length gets deleted

iis header Content-length