【问题标题】:Writing binary data to ftp location将二进制数据写入 ftp 位置
【发布时间】:2013-12-07 15:09:40
【问题描述】:

这段代码怎么会在给定位置写入一个空文件? 没有错误消息。

// 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 循环。

【问题讨论】:

    标签: c# asp.net stream filestream binarywriter


    【解决方案1】:

    你应该在 myStreamWriter 关闭后调用upload.GetResponse()

    PS:在你每读两次写一次的同时,真的是你想要的吗?

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多