最近用swfupload做图片上传,发现cookie值无法取得。
使用firefox和chrome无法取得cookie值。
调查之,发现时flashplayer的bug,windows下非IE浏览器上传的实际上也是IE浏览器的cookie值。而且到现在adobe也没有要解决这个东西的意图。
Cookie issue
On Windows the Non-IE Flash Player plugin (FireFox, Opera, Safari, etc) sends the IE cookies regardless of the browser used. This breaks authentication and sessions for many server-side scripting technologies.
Developers should manually pass Session and Authentication cookie information and manually restore Sessions on the Server Side if they wish to use Sessions
The SWFUpload package contains work-around sample code for PHP and ASP.Net
解决办法无非只能是把cookie通过request参数传递到服务器端。
swfupload提供了一个插件,swfupload.cookies.js。
Cookies
In response to the Flash Cookie Bug the Cookies Plugin automatically retrieves your browser's cookies and sends them with the uploads. The are sent as POST or GET variables to the upload url.
这个插件会自动帮你把cookie的值转换并设置到request的param中。
注:如果自己也需要向request中设置param的话,不要使用它提供的setPostParams方法,这样会将cookie插件设置的cookie值覆盖掉,只能通过addPostParam方法追加(注意方法名,顾名思义,只能一次追加一个参数)。
引入这个js即可。
然后在action中自己取得request中的参数即可。
另外一个注意点:原始cookie是通过文件头传递的,而上面的处理方式就变成url传递了,所以url里面某些字符被转义了,在action中取得后要进行decode操作才行。