swfupload提示“错误302”的解决方法

时间:2021-06-02 14:34:44

1、关于图片上传控件,flash控件的显示效果要好一些,本人使用swfupload

2、swfupload上传控件使用方式详见文档 http://www.leeon.me/upload/other/swfupload.html

3、参照文档说明和官方demo,写一个小例子是没有任何问题的,在firefox或者chrome下如碰到302错误,错误堆栈如下:

SWF DEBUG: SWFUpload Init Complete
SWF DEBUG:
SWF DEBUG: ----- SWF DEBUG OUTPUT ----
SWF DEBUG: Version: 2.5.0 2010-03-05 Beta 3.2
SWF DEBUG: movieName: SWFUpload_0
SWF DEBUG: Upload URL: /iwebshop/index.php?controller=shop&action=goods_imgupload&photo_name=photo_name&showlist=show_list&single=false
SWF DEBUG: File Types String: *.jpg;*.jpge;*.png;*.gif
SWF DEBUG: Parsed File Types: jpg,jpge,png,gif
SWF DEBUG: HTTP Success: 0
SWF DEBUG: File Types Description: JPG Images (*.jpg;*.jpge;*.png;*.gif)
SWF DEBUG: File Size Limit: 2097152 bytes
SWF DEBUG: File Upload Limit: 5
SWF DEBUG: File Queue Limit: 5
SWF DEBUG: Post Params:
SWF DEBUG: PHPSESSID=feec128121ce54aa04f6b4a249bd5bc9
SWF DEBUG: ----- END SWF DEBUG OUTPUT ----
SWF DEBUG:
SWF DEBUG: Stage Resize:50 by 21
SWF DEBUG: Event: fileDialogStart : Browsing files. Multi Select. Allowed file types: *.jpg;*.jpge;*.png;*.gif
SWF DEBUG: Select Handler: Received the files selected from the dialog. Processing the file list...
SWF DEBUG: Event: fileQueued : File ID: SWFUpload_0_0
SWF DEBUG: Event: fileDialogComplete : Finished processing selected files. Files selected: 1. Files Queued: 1
SWF DEBUG: StartUpload: First file in queue
SWF DEBUG: Event: uploadStart : File ID: SWFUpload_0_0
SWF DEBUG: StartUpload(): Upload Type: Normal.
SWF DEBUG: Global Post Item: PHPSESSID=feec128121ce54aa04f6b4a249bd5bc9
SWF DEBUG: ReturnUploadStart(): File accepted by startUpload event and readied for standard upload. Starting upload to /iwebshop/index.php?controller=shop&action=goods_imgupload&photo_name=photo_name&showlist=show_list&single=false for File ID: SWFUpload_0_0
SWF DEBUG: Event: uploadProgress (OPEN): File ID: SWFUpload_0_0 Bytes: 0. Total: 98168
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 65536. Total: 98168
SWF DEBUG: Event: uploadProgress: File ID: SWFUpload_0_0. Bytes: 98168. Total: 98168
SWF DEBUG: Event: uploadError: HTTP ERROR : File ID: SWFUpload_0_0. HTTP Status: 302.
EXCEPTION:
SWF DEBUG: Event: uploadComplete : Upload cycle complete.

红色文字部分就是返回的错误信息,出现此错误的原因有以下几个:

a 、flash上传图片时,会另起一个线程上传,这个线程中没有cookie,session等会话信息,包括自定义的全局变量,调用上传的url路径后台进行处理时,如果后台进行了权限或者登陆校验就会校验失败,后台校验失败就会重定向登录页面,flash这边就会报出302错误,但是上传界面不会发生跳转。

b、如果程序使用了系统框架,一般框架会有过滤器进行权限校验或者session校验,对于框架不熟悉的人可能不知道为什么发生302错误,也不知道在哪处理过滤,导致无法解决问题,这个需要仔细研究框架了。

c、对于swfupload的demo可以直接部署运行,而不报错,就是因为接收图片界面没有校验session或者权限。

知道原因后,问题就很好解决,根据MVC项目中的身份验证节点,如下所示:

    <authentication mode="Forms">
<forms name="USER_SESSID" loginUrl="~/Admin/Account/Login" protection="All" timeout="10080" path="/" requireSSL="false" slidingExpiration="false" enableCrossAppRedirects="false" cookieless="UseCookies" domain="" />
</authentication>

在上传图片时,需要把session ID Post过去,因为swfupload已经封装了此功能,在配置swfupload时添加 post_params 属性即可,实现代码如下:

post_params: { "ASPSESSID": "@Request.Cookies["USER_SESSID"].Value" },