3

I used this code to upload image to ftp. but image is corrupted. image im trying to upload is a Base64 string.i converted to stream and passed it to UpLoadImage.

 public static void UpLoadImage(Stream image, string target)
   {
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://www.examp.com/images/" + target);
    req.UseBinary = true;
    req.Method = WebRequestMethods.Ftp.UploadFile;
    req.Credentials = new NetworkCredential("UserNm", "PassWd");
    StreamReader rdr = new StreamReader(image);
    byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd());
    rdr.Close();
    req.ContentLength = fileData.Length;
    Stream reqStream = req.GetRequestStream();
    reqStream.Write(fileData, 0, fileData.Length);
    reqStream.Close();
}

Instead of:

StreamReader rdr = new StreamReader(image); byte[] fileData = Encoding.UTF8.GetBytes(rdr.ReadToEnd()); rdr.Close();

if i use byte[] fileData = File.ReadAllBytes(image); it gives me a error, filename is to more the 260 character.

Please can someone help to solve this..

KF2
  • 9,887
  • 8
  • 44
  • 77
user2226345
  • 33
  • 1
  • 4

2 Answers2

2

Like a comment say use

byte[] fileData = File.ReadAllBytes(filePath);

for all file that are not text file.

like that:

private void FTPUPLOAD(string imgPath)
        {
            FTPDelete(img);
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(BaseUrl +Path.GetFileName(imgPath));
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential(Account, Password);

            // Copy the contents of the file to the request stream.
             //THIS IS THE CODE
            byte[] fileContents = File.ReadAllBytes(imgPath);

 Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            response.Close();
        }
abrfra
  • 634
  • 2
  • 6
  • 24
1

You should be using a Stream to read binary files, not a StreamReader. StreamReader is designed to read text files only.

and about error Maximum Path Length Limitation

Maximum Path Length Limitation In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

path limition:http://msdn.microsoft.com/en-us/library/system.io.pathtoolongexception.aspx

Edited

I write a simple one: Convert image to b64 and back

//Convert image to b64
            string path = @"E:\Documents and Settings\Ali\Desktop\original.png";
            Image img = Image.FromFile(path);
            byte[] arr;
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, ImageFormat.Jpeg);
                arr = ms.ToArray();
            }
            String b64 = Convert.ToBase64String(arr);//result:/9j/4AAQSkZJRgABAQEA...
            //Get image bytes
            byte[] originalimage= Convert.FromBase64String(b64);

send b64 to your function and convert it back byte[] originalimage= Convert.FromBase64String(b64);

KF2
  • 9,887
  • 8
  • 44
  • 77
  • 1
    byte[] fileData = File.ReadAllBytes(image) requires a string as parameter. – user2226345 Apr 05 '13 at 05:07
  • @user2226345:File.ReadAllBytes("address of your image"),i think your path is >260 charachter – KF2 Apr 05 '13 at 05:10
  • when i pass a base64 string to File.ReadAllBytes(image) it shows an error that the filename or path is to long(has more than 260 character) – user2226345 Apr 05 '13 at 05:12
  • your path is >260 character – KF2 Apr 05 '13 at 05:15
  • try this http://stackoverflow.com/questions/1228701/code-for-decoding-encoding-a-modified-base64-url – KF2 Apr 05 '13 at 05:30
  • image name encoded with Base64 string?could you add part of code? – KF2 Apr 05 '13 at 05:57
  • public string Test(String url1) { byte[] byteArray = Encoding.UTF8.GetBytes(url1); MemoryStream stream = new MemoryStream(byteArray); UpLoadImage(url1,"DSdiagnosis.jpg"); – user2226345 Apr 05 '13 at 06:27
  • that is the funtion which calls UpLoadImage(). where url1 is canvas image which being accessed using webservice. – user2226345 Apr 05 '13 at 06:30
  • you are encode your URL like this: `byte[] url = Encoding.UTF8.GetBytes("c:\\original.png");` so after you send stream address to upload function get original URL again with `File.ReadAllBytes(Encoding.UTF8.GetString(url));` – KF2 Apr 05 '13 at 06:52
  • this is the content of image image= "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAAAAAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCADIA5MDASIAAhEBA... – user2226345 Apr 05 '13 at 06:57
  • Sorry there is a change in the code --> public string Test(String url1) { byte[] byteArray = Encoding.UTF8.GetBytes(url1); MemoryStream stream = new MemoryStream(byteArray); UpLoadImage(stream,"DSdiagnosis.jpg"); – user2226345 Apr 05 '13 at 07:05
  • File.ReadAllBytes(Encoding.UTF8.GetString(url)); to use this parameter should be a byte[].so when i convert url to byte[] and pass,it gives the same error PathTooLong. – user2226345 Apr 05 '13 at 08:41
  • `byte[] originalimage= Convert.FromBase64String(b64);`this line is the same as `File.ReadAllBytes(Encoding.UTF8.GetString(url));` instead od using `File.ReadAllBytes` use my code – KF2 Apr 05 '13 at 08:47
  • the error has changed to :The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters. – user2226345 Apr 05 '13 at 08:53