What's wrong in this code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Rogers

    What's wrong in this code?

    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_Clic k(object sender, System.EventArg s e) {
    WebRequest req = WebRequest.Crea te(txbFile.Text );

    try {
    WebResponse result = req.GetResponse ();
    Stream ReceiveStream = result.GetRespo nseStream();
    if(true /* TODO: condition*/) {
    FileStream newFile = new FileStream(Reso lvePath("temp.z ip"),
    FileMode.Create );

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

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

    Thanks,
    - Michael


Working...