user1655444
user1655444

Reputation: 1

How do I set the encoding for a .net binary file transfer

I can successfully upload text files using .net ftp but now want to upload a graphics (.png) image. How should I set the stream encoding ?

What I have for text (in Apl script ) :

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

I then lose bytes from the transferred binary file. I have also tried omitting the encoding line.

cheers, Beau

More info : This is the core of the Ascii version, written in AplX .net - It works fine - now I need a BinaryReader version. Help appreciated

  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
  .....

Here is what I have now been trying, based on vb code at :

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

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

So I have tried :

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

Here are the error messages :

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

Upvotes: 0

Views: 1297

Answers (1)

Dai
Dai

Reputation: 155250

Binary files don't have "encoding" unless you want to transform the data to survive a lossy transfer medium (such as 7-bit BBSes or MTAs, where you'd want to Base64 encode your data). Encoding is for text files.

Don't use StreamReader or StreamWriter for working with binary data. The classes aren't named well, they should be called TextStreamReader and TextStreamWriter to better reflect what they do. If you've got a Stream you want to work with then you should use BinaryReader and BinaryWriter.

Upvotes: 1

Related Questions