This project is read-only.

SendFile upload an image but this can not be displayed

Jan 22, 2013 at 3:56 AM

SendFile upload an image but this can not be displayed. Using the example that brings the library in C #. The image is uploaded to a bit less than the original. I could not open the project because I asked Conéctame tfc.codeplex.com. So I took file and create a new project.
What can be the problem?.
thanks

Jan 24, 2013 at 6:42 PM
I found the problem. These are the changes to work well OpenNETCF. Make changes to the methods following methods:

  • OpenDataSocket()

 

char ch = ipstring[i];
replace by
 
char chr = char.Parse(ipData.Substring(i, 1)); 

  and

 

IPHostEntry ipHost = Dns.GetHostByName(Dns.GetHostName());
long ip = ipHost.AddressList[0].Address;

 

Replace by (Methods. NET Framework 4 are obsolete).

 

IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
long ip = long.Parse(ipHost.AddressList[0].ToString());

 

  • EnumFiles()

case FTPServerType.Unix:

 

 lines[l] = lines[l].Substring(30).Trim()

replace by

 lines[l] = lines[l].Substring(36).Trim();

  • SendFile ()

The changes are many, do not have to spend RemoteFileName. And the sums bool parameter indicates if we overwrite (true) or do not want to overwrite (false) the. Overwritten if the file is a different size.
Add a couple of helper methods

 

        /// <summary>
  /// Upload a file to the FTP server and set the resume flag. /// </summary> /// <param name="fileName"></param> /// <param name="resume"></param> /// public void SendFile(string fileName, bool resume) { //make sure the user is logged in CheckConnect(); Socket dataSocket = null; long resumeOffset = 0; //if resume is true if (resume) { try { //set _isBinary to true IsBinary = true; //get the size of the file resumeOffset = GetFileSize(Path.GetFileName(fileName)); } catch (Exception) { // file not exist //Console.WriteLine(ex); resumeOffset = 0; } } // open stream to read file FileStream input = new FileStream(fileName, FileMode.Open); //if resume is true //and the size of the file read is //less than the initial value if ((resume && input.Length < resumeOffset) || (resume && input.Length > resumeOffset)) { // different file size //Console.WriteLine("Overwriting " + fileName, "FtpClient"); resumeOffset = 0; } else if (resume && input.Length == resumeOffset) { // file done input.Close(); //Console.WriteLine("Skipping completed " + fileName + " - turn resume off to not detect.", "FtpClient"); return; } //now create our socket needed for //the file transfer dataSocket = OpenDataSocket(); //dataSocket = OpenSocketForTransfer(); //if the file size is greater than 0 if (resumeOffset > 0) { //execute the rest command, which //sets the point the resume will occurr //if the upload is interrupted Execute("REST " + resumeOffset); //check the status code, if it's not //350 the resume isnt supported by the server if (_statusCode != 350) { //Console.WriteLine("Resuming not supported", "FtpClient"); resumeOffset = 0; } } //execute the store ftp command (starts the transfer of the file) Execute("STOR " + Path.GetFileName(fileName)); //check the status code, we need a //value of 150 or 125, otherwise throw an exception if (_statusCode != 125 && _statusCode != 150) { //Console.WriteLine("Status 125 o 150"); throw new FTPException(result.Substring(4)); } //now check the resumeOffset value, //if its not zero then we need to resume //the upload process where it ended if (resumeOffset != 0) { //let the user know the upload is resuming //Console.WriteLine("Resuming at offset " + resumeOffset, "FtpClient"); //use the Seek method to get to where the upload ended input.Seek(resumeOffset, SeekOrigin.Begin); } //let the user know the uploading has begun //Console.WriteLine("Uploading file " + fileName + " to " + m_host, "FtpClient"); //upload the file while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0) { dataSocket.Send(buffer, bytes, 0); } //close our reader input.Close(); //check to see if the socket is still connected //if it is then disconnect it if (dataSocket.Connected) { dataSocket.Close(); } //read the host's response readResponse(); //checking for a successful upload code (226 or 250) //if not either then throw an exception if (_statusCode != 226 && _statusCode != 250) { //Console.WriteLine("Status 226 o 250"); throw new FTPException(result.Substring(4)); } }
        /// <summary>
        /// Method to retrieve the size of the file based
        /// on the name provided
        /// </summary>
        /// <param name="file">Name of the file to get the size of</param>
        /// <returns>The files size</returns>
        public long GetFileSize(string file)
        {
            //make sure the user is logged in

            //execute the size command, which
            //returns the files size as a decimal number
            Execute("SIZE " + file);
            long fileSize = 0;
            //check our returning status code
            //if it's not 213 the command failed
            if (_statusCode == 213)
            {
                //set the file size
                fileSize = long.Parse(result.Substring(4));
            }
            else
            {
                //command failed so throw an exception

                //Console.WriteLine("Command failed so throw an exception");
                throw new FTPException(result.Substring(4));
            }
            //return the file size
            return fileSize;
        }

 

 

        /// <summary>
        /// method to send the ftp commands to the remove server
        /// </summary>
        /// <param name="command">the command to execute</param>
        private void Execute(String msg)
        {
            //convert the command to a byte array
            Byte[] cmdBytes = Encoding.ASCII.GetBytes((msg + "\r\n").ToCharArray());
            //send the command to the host
            m_cmdsocket.Send(cmdBytes, cmdBytes.Length, 0);
            //read the returned response
            readResponse();
        }


private
int _statusCode = 0; private string result = string.Empty; private void readResponse() { result = ParseHostResponse(); _statusCode = int.Parse(result.Substring(0, 3)); }