0

このコードが特定の場所に空のファイルを書き込むのはなぜですか? エラー メッセージはありません。

// upload file
WebRequest upload = WebRequest.Create(ftp + path + "/" + file);
upload.Method = WebRequestMethods.Ftp.UploadFile;
upload.Credentials = new NetworkCredential(username, password);

String filePath = HttpContext.Current.Server.MapPath("~/temp/" + file); // path to file to upload

Stream myReadStream = new FileStream(filePath, FileMode.Create); // stream that can read binary data

BinaryWriter myStreamWriter = new BinaryWriter(upload.GetRequestStream()); // writer that can write the binary data to the FTP server

while (myReadStream.ReadByte() != -1)
{
    myStreamWriter.Write(myReadStream.ReadByte());
}

myStreamWriter.Close();
myReadStream.Close();

while ループを削除すると、4 バイトのファイルが作成されて破損するため、このように while ループに入ることはできないと思います。

4

1 に答える 1

0

upload.GetResponse()myStreamWriter を閉じた後に呼び出す必要があります。

PS: あなたは 2 回読むたびに 1 回書きますが、本当にそれを望んでいますか?

于 2013-12-07T15:53:41.057 に答える