0

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'
2
  • 1
    Use UTF8 encoding. ASCII encoding will cause problems and you will end up pulling your hair out trying to fix this (It happened to me) Commented Sep 7, 2012 at 18:26
  • what does your ftp code look like? Commented Sep 7, 2012 at 18:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hmm I am still having problems. I gather this should look like : new BinaryReader(File.Open(fileName, FileMode.Open)); Can anyone give me an idea of what this looks like one step at a time, I am having problems translating it into object based AplX, thanks, Beau
The Ascii version, which works fine - now I need a BinaryReader version : 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 .....
` 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 ..... `
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 .....
<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 ..... </code>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.