473,382 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Sending a zip file with Content-Disposition issue: corrupt (half-length; strange!)

Hi,

I'm trying to send a zipfile to a client using Content-Disposition:
attachment. Done it before and it works fine.

My code is below:

context.Response.Buffer = false;
System.IO.MemoryStream stream =
ArchiveUtil.CreateArchive(new string[] { "c:\\windows\
\setuplog.txt" } , new string[] { "setuplog.txt" });
context.Response.ContentType = "application/octet-
stream";
context.Response.AddHeader("Content-Disposition",
"attachment; filename=\"test-download.zip\"");
context.Response.AddHeader("Content-Length",
stream.Length.ToString());

byte[] buffer = new byte[1024];
long byteCount;
int total = 0;

while ((byteCount = stream.Read(buffer, 0,
(int)buffer.Length)) 0)
{
if (context.Response.IsClientConnected)
{

context.Response.OutputStream.Write(buffer, 0, (int)byteCount);
total += (int)byteCount;
context.Response.OutputStream.Flush();
}
else
break;
}

stream.Close();
context.Response.End();
(The MemoryStream is fine and valid - saving the contents to disk
works fine.)

The problem is the stream contains around 60k-ish bytes, but IE/
Firefox/whatever only gets about 30k. At the end of the code, "total"
contains the correct value (60k).

The code above infact causes the download to 'hang' in IE - because
Content-Length is set to 60k. It is only by removing the Content-
Length header that I actually get a file in IE I can open/save; and
it's corrupted and only 30k.

Looking at the corrupt file in a hex editor, it has "PK" and the
setuplog.txt filename near the beginning and again near the end.
Comparing this with the "correct" zip file, this is the same! It is
NOT simply truncated. It's like the binary data in the file is being
transformed somehow?

Putting the code in a clean/fresh ASP.NET app, it annoyingly works
fine. As far as I know there are no weird settings in the main app
where this code sits.

After looking carefully at the session with Fiddler (comparing the
working miniapp with this broken one), apart from the obvious
difference that the wrong number of bytes are actually sent down the
socket, the non-working code sends using CHUNKED content encoding -
the working one doesn't. I have no idea why this is or if it might be
a cause/effect of the problem?

Does anyone have any ideas at all? Heard of the binary data being
transformed in some way before? This is driving me crazy... any input
appreciated!

Thanks

Mar 13 '07 #1
1 5967
On 13 Mar, 11:19, kier...@gmail.com wrote:
Hi,

I'm trying to send a zipfile to a client using Content-Disposition:
attachment. Done it before and it works fine.

My code is below:

context.Response.Buffer = false;
System.IO.MemoryStream stream =
ArchiveUtil.CreateArchive(new string[] { "c:\\windows\
\setuplog.txt" } , new string[] { "setuplog.txt" });
context.Response.ContentType = "application/octet-
stream";
context.Response.AddHeader("Content-Disposition",
"attachment; filename=\"test-download.zip\"");
context.Response.AddHeader("Content-Length",
stream.Length.ToString());

byte[] buffer = new byte[1024];
long byteCount;
int total = 0;

while ((byteCount = stream.Read(buffer, 0,
(int)buffer.Length)) 0)
{
if (context.Response.IsClientConnected)
{

context.Response.OutputStream.Write(buffer, 0, (int)byteCount);
total += (int)byteCount;
context.Response.OutputStream.Flush();
}
else
break;
}

stream.Close();
context.Response.End();

(The MemoryStream is fine and valid - saving the contents to disk
works fine.)

The problem is the stream contains around 60k-ish bytes, but IE/
Firefox/whatever only gets about 30k. At the end of the code, "total"
contains the correct value (60k).

The code above infact causes the download to 'hang' in IE - because
Content-Length is set to 60k. It is only by removing the Content-
Length header that I actually get a file in IE I can open/save; and
it's corrupted and only 30k.

Looking at the corrupt file in a hex editor, it has "PK" and the
setuplog.txt filename near the beginning and again near the end.
Comparing this with the "correct" zip file, this is the same! It is
NOT simply truncated. It's like the binary data in the file is being
transformed somehow?

Putting the code in a clean/fresh ASP.NET app, it annoyingly works
fine. As far as I know there are no weird settings in the main app
where this code sits.

After looking carefully at the session with Fiddler (comparing the
working miniapp with this broken one), apart from the obvious
difference that the wrong number of bytes are actually sent down the
socket, the non-working code sends using CHUNKED content encoding -
the working one doesn't. I have no idea why this is or if it might be
a cause/effect of the problem?

Does anyone have any ideas at all? Heard of the binary data being
transformed in some way before? This is driving me crazy... any input
appreciated!

Thanks
Anyone have any ideas? Forgive me for bumping this but it's a decent
question, not obvious, and would be helpful to get answered :)
Mar 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jay | last post by:
This is a strange one that I can't seem to find a fix for. We have a Billing DB application (Access 2000 format) where we upload billing info in a comma delimited text file to our printer who...
2
by: Trygve Lorentzen | last post by:
Hi, we are in these days deploying a .NET windows app. The exe file is meant to be self-updating (custom built logic) by downloading newer versions when a new version is built. There is a...
4
by: hb | last post by:
I am having trouble with creating a document server from a database using asp.net. It works on the development machine (win2k, iis5.0), but fails on the production server (win2003, iis6.0). The...
1
by: Henk | last post by:
Hi, I have a problem with sending a file. I have a client application that connects to a server and sends the string "data". Then it sends a file. If the server receives "data", the method...
3
by: darrel | last post by:
In our CMS, every time a page is updated, we spit out a new XML file. This file is written via a function that queries the Database using a recursive function, grabs the data, and writes out a...
4
by: AtulSureka | last post by:
Hi, I have file of relatively larger size (4-5 Mbs) on client machine. I want to send this file to Server. For this I am using web services. Following is my perception, Please correct me if I...
1
by: Mike | last post by:
I am using PowerShell to download an XML file into a string using the DownloadString(URI) method of System.Net.WebClient object. Sample commands below: $Result = $Null; $WebClient = new-object...
1
by: Obaum1 | last post by:
Hello I am sending a file to a webservice after i transform him to byte And when i am sending a very large file(400 MB) i get an Exception of type 'System.OutOfMemoryException' was thrown. ...
7
by: koneyji | last post by:
hi i have following code to send file and its giving file io error namespace client_server { class Program { static void Main(string args) { IPAddress ip...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.