Uploading and downloading files in the proper mode is important. Find out why it matters, and which should be transferred as ascii & which as binary.
Ascii or Binary?
The general rule of thumb is if you can view the file in a text editor like notepad (ie. .html, .js, .css files etc) you should upload in ASCII mode, most others (including images, sound files, video, zip files, executable's etc) should be uploaded in Binary.
Exceptions to the Rule
It seems all things related to computers have exceptions to the rules. Yes, this is yet another case of it.
If your text files contain international characters (ie. Chinese or Japanese text), they are to be uploaded as binary. The reason is that ascii takes into account differences between DOS and UNIX files (7 bits) but it doesn't do well with text using higher bits.
Why Does It Matter What Mode You Transfer Files With?
If you upload images etc. as ascii you'll end up with corrupted files. Some browsers seem capable of figuring it out, but not all... and not all the time. Netscape is much more picky, so you'd end up with broken or missing pictures for your Netscape users. (Yup, I learned this the hard way :-o )
Same thing with uploading text files as binary. While this is less important for html files, scripts will have a HUGE problem with it and will just not work. This is the most common cause of the "Server 500 Error - Malformed Headers", and other equally unlovely errors that have caused many a webmaster to bang their heads against their computers.
So What Does Uploading In ASCII Do?
I'd wondered this for a while and finally decided to learn why uploading in ascii is so important for some file types. What I found out is that different Operating System's use different ways to specify that a line has ended. So if you're using a different operating system than your server (which is very likely), the files will have extra characters at the end of each line that the server doesn't recognize. So it'll usually print them out resulting in script errors.
Setting Your FTP Program to "Auto"
Most FTP programs have the option to set your upload to auto. What this usually does is compare the file type you're transferring against a list of known file types and set it to binary or ascii upload on its own.
By default, most FTP programs will have a pre-set list of files to be transferred in ascii and will upload / download everything else in binary. (These settings are in different places depending on the program you are using. Check the "Read Me" file or their Website if you can't find it.) Be sure to double check that the files you want to transfer are in the appropriate list.
Summary
ASCII Files
.htm .html .shtml .php .pl .cgi .js .cnf .css
.forward .htaccess .map .pwd .txt .grp .ctl
Binary Files
.jpg .gif .png .tif .exe .zip .sit .rar .ace
.class .mid .ra .avi .ocx .wav .mp3 .au
FTP可用多种格式传输文件,通常由系统决定,大多数系统(包括UNIX系统)只有两种模式:文本模式和二进制模式。文本传输器使用ASCII字符,并由回车键和换行符分开,而二进制不用转换或格式化就可传字符,二进制模式比文本模式更快,并且可以传输所有ASCII值,所以系统管理员一般将FTP设置成二进制模式。
一般来说:
如果你用错误的模式传输你的图片,你将会无法看到图片,看到的会是乱码。
如果你用错误模式上传CGI脚本,那么就将无法运行你的脚本,会看到类似Server 500 Error的出错信息。
所以你必须使用正确的模式,图片和执行文件必须用BINARY模式,CGI脚本和普通HTML文件用ASCII模式上传.
ASCII和BINARY模式区别:
用HTML 和文本编写的文件必须用ASCII模式上传,用BINARY模式上传会破坏文件,导致文件执行出错。
BINARY模式用来传送可执行文件,压缩文件,和图片文件。
如果你用ASCII模式传,会显示一堆乱码,你必须重新用BINARY模式传。
对于第二种情况,是因为有很多ftp服务器和客户端软件能自动识别文件类型,并采取相应的传输方式。
ftp是应用层协议,和具体操作系统无关 .
ASCII模式和BINARY模式的区别是回车换行的处理,binary模式不对数据进行任何处理,asci模式将回车换行转换为本机的回车字符,比如Unix下是n,Windows下是rn,Mac下是r
ascii模式下会转换文件
不能说是不同系统对回车换行解释不同
而是不同的系统有不同的行结束符
unix系统下行结束符是一个字节,即十六进制的0A
而ms的系统是两个字节,即十六进制的0D0A
所以当你用ascii方式从unix的ftp server下载文件时(不管是二进制或者文本文件),每检测到一个
字节是0A,就会自动插入一个0D,所以如果你的文件是二进制文件比如可执行文件、压缩包什么的,就肯
定不能用了。如果你的文件就是unix下的文本文件,你用ascii模式是正确的,要是误用了binary模式,
你在windows上看这个文件是没有换行的,里面是一个个的黑方块。
一般来说,我们最好都用binary方式,这样可以保证不出错。如果有文本格式转换的问题,即unix格式
的文本和dos格式的文本之间的转换,有很多工具可以做的,不要在ftp传输的时候冒险,尤其是你如果对
这些东西不是非常清楚的话。
可以使用MIME,把所有的字符,转换成0~128之间的字符,然后传送,在接受方再将接收到的字符MIME反向转换。通常我们发送邮件,就是使用这样的字符转换方式。
from : http://shikary.blog.sohu.com/53940630.html




最新回复