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:在你每读两次写一次的同时,真的是你想要的吗?

于 2013-12-07T15:53:41.057 回答