c# - image corrupted when Uploading to ftp using ftpwebrequest -
i used code upload image ftp. image corrupted. image im trying upload base64
string.i converted stream , passed 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 use byte[] filedata = file.readallbytes(image);
gives me error, filename more 260 character.
please can solve this..
you should using stream read binary files, not streamreader. streamreader designed read text files only.
and error maximum path length limitation
maximum path length limitation in windows api (with exceptions discussed in following paragraphs), maximum length path max_path, defined 260 characters. local path structured in following order: drive letter, colon, backslash, name components separated backslashes, , terminating null character. example, maximum path on drive d "d:\some 256-character path string" "" represents invisible terminating null character current system codepage. (the characters < > used here visual clarity , cannot part of valid path string.)
path limition:http://msdn.microsoft.com/en-us/library/system.io.pathtoolongexception.aspx
edited
i write simple one: convert image b64 , back
//convert image b64 string path = @"e:\documents , 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 function , convert byte[] originalimage= convert.frombase64string(b64);
Comments
Post a Comment