What's wrong in this code?

M

Michael Rogers

I wrote a simple file transfer, but it doesn't work!
When I try to open the transferred file it says: "The end of file cropped"
or "the file header cropped". what's wrong with this code? IS there a better
approach?

private void btnGetFile_Click(object sender, System.EventArgs e) {
WebRequest req = WebRequest.Create(txbFile.Text);

try {
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
if(true /* TODO: condition*/) {
FileStream newFile = new FileStream(ResolvePath("temp.zip"),
FileMode.Create);

Byte[] read = new Byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);

while (bytes > 0) {
newFile.Write(read, 0, 512);
bytes = ReceiveStream.Read(read, 0, 512);
}
newFile.Close();
lblResult.ForeColor = Color.Black;
lblResult.Text = "Operation Succeed!";
btnShow.Text = "Download File!";
btnShow.Enabled = true;
}
} catch(Exception _ex) {
lblResult.Text = _ex.ToString();
}
}

Thanks,
- Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top