【问题标题】:How do I set the encoding for a .net binary file transfer如何设置 .net 二进制文件传输的编码
【发布时间】:2012-09-07 18:25:03
【问题描述】:

我可以使用 .net ftp 成功上传文本文件 但现在想上传一个图形 (.png) 图像。 我应该如何设置流编码?

我拥有的文本(在 Apl 脚本中):

sourceStream ← '.net' ⎕new 'System.IO.StreamReader' 本地路径文件名 编码←'.NET' ⎕NEW 'System.Text.ASCIIEncoding'

然后我会从传输的二进制文件中丢失字节。 我也试过省略编码行。

干杯,美女

更多信息: 这是 Ascii 版本的核心,用 AplX .net 编写 - 它工作正常 - 现在我需要一个 BinaryReader 版本。 帮助表示赞赏

  ftp←'.net' ⎕call 'System.Net.WebRequest.Create' Remotepathfilename
  ftp.Method←'.net' ⎕call 'System.Net.WebRequestMethods+Ftp.UploadFile'
  ftp.Credentials←'.net' ⎕new 'System.Net.NetworkCredential' Username Password

sourceStream ← '.net' ⎕new 'System.IO.StreamReader' Localpathfilename
encoding←'.NET' ⎕NEW 'System.Text.ASCIIEncoding'

fileContents ← encoding.GetBytes.⎕REF 
sourceStream.ReadToEnd 
sourceStream.Close 
ftp.ContentLength ← fileContents.Length 
:try
  stream←ftp.GetRequestStream
:catchall
  .....

这是我现在一直在尝试的,基于 vb 代码:

http://msdn.microsoft.com/en-us/library/system.io.file.openread#Y1035

Dim fs As FileStream
FileStream fs = File.OpenRead(path))

所以我试过了:

fileStream ← '.net' ⎕new 'System.IO.File' 
fileStream.OpenRead  Localpathfilename

以下是错误消息:

Constructor on type 'System.IO.File' not found.
DOMAIN ERROR
net_ftp_putfile[72] fileStream←'.net' ⎕new 'System.IO.File'

【问题讨论】:

  • 使用 UTF8 编码。 ASCII 编码会导致问题,你最终会为了解决这个问题而费尽心思(这发生在我身上)
  • 你的 ftp 代码是什么样的?

标签: .net encoding ftp binary


【解决方案1】:

二进制文件没有“编码”,除非您希望转换数据以在有损传输介质(例如 7 位 BBS 或 MTA,您希望对数据进行 Base64 编码)中存活。编码用于文本文件。

不要使用StreamReaderStreamWriter 处理二进制数据。这些类没有很好地命名,它们应该被称为TextStreamReaderTextStreamWriter 以更好地反映它们的作用。如果您有想要使用的 Stream,那么您应该使用 BinaryReaderBinaryWriter

【讨论】:

  • 啊,真的谢谢,我去试试,非常感谢。
  • 嗯,我还是有问题。我收集这应该看起来像: new BinaryReader(File.Open(fileName, FileMode.Open));谁能给我一个关于这一步一步的想法,我在将其转换为基于对象的 AplX 时遇到问题,谢谢,Beau
  • Ascii 版本,工作正常 - 现在我需要一个 BinaryReader 版本:ftp←'.net' ⎕call 'System.Net.WebRequest.Create' Remotepathfilename ftp.Method←'.net' ⎕ call 'System.Net.WebRequestMethods+Ftp.UploadFile' ftp.Credentials←'.net' ⎕new 'System.Net.NetworkCredential' 用户名密码 sourceStream ← '.net' ⎕new 'System.IO.StreamReader' Localpathfilename encoding←' .NET' ⎕NEW 'System.Text.ASCIIEncoding' fileContents ← encoding.GetBytes.⎕REF sourceStream.ReadToEnd sourceStream.Close ftp.ContentLength ← fileContents.Length :try stream←ftp.GetRequestStream :catchall .....
  • ` ftp←'.net' ⎕call 'System.Net.WebRequest.Create' Remotepathfilename ftp.Method←'.net' ⎕call 'System.Net.WebRequestMethods+Ftp.UploadFile' ftp。 Credentials←'.net' ⎕new 'System.Net.NetworkCredential' 用户名密码 sourceStream ← '.net' ⎕new 'System.IO.StreamReader' Localpathfilename encoding←'.NET' ⎕NEW 'System.Text.ASCIIEncoding' fileContents ← encoding.GetBytes.⎕REF sourceStream.ReadToEnd sourceStream.Close ftp.ContentLength ← fileContents.Length :try stream←ftp.GetRequestStream :catchall .....`
  • code ftp←'.net' ⎕call 'System.Net.WebRequest.Create' Remotepathfilename ftp.Method←'.net' ⎕call 'System.Net.WebRequestMethods+Ftp.UploadFile' ftp.Credentials←'.net' ⎕new 'System.Net.NetworkCredential' Username Password sourceStream ← '.net' ⎕new 'System.IO.StreamReader' Localpathfilename encoding←'.NET' ⎕NEW 'System.Text.ASCIIEncoding' fileContents ← encoding.GetBytes.⎕REF sourceStream.ReadToEnd sourceStream.Close ftp.ContentLength ← fileContents.Length :try stream←ftp.GetRequestStream :catchall .....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
相关资源
最近更新 更多