asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error

时间:2022-10-22 19:46:11

CSHTML代码

 @{
        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
        {
            <input type="hidden" id="cookieVal" name="cookieVal" value="@Request.Cookies[FormsAuthentication.FormsCookieName].Value" />;
        }
    }

    <script type="text/javascript">
        $(function () {
            var auth = $("#cookieVal").val() || "";
            $("#uploadify").uploadify({
                'swf': '../../Content/uploadify/uploadify.swf',
                'uploader': "/LawInfo/Uploadify",
                'cancelImg': '../../Content/uploadify/cancel.png',
                'folder': '../../File/uploadLaw',
                'queueID': 'fileQueue',
                'buttonText': "浏览",
                'fileTypeExts': '*.doc;*.docx;*.pdf;*.xls;*.xlsx;*.txt;*.ppt;*.pptx',
                'auto': false,
                'formData': { 'AUTHID': auth, 'ASPSESSID': '@Session.SessionID' },
                'fileSizeLimit': 10485760,
                'removeTimeout': 1,
                'multi': false,
                'onSelect': function () {
                    $("#fileQueue").show();
                    //$("#_dialog").show();
                },
                'onUploadSuccess': function (file, data, response) {
                    var jsonres = JSON.parse(data);
                    $("#previewdiv").show();
                    //$("#Icon").val(jsonres.filename);//为隐藏域赋值
                    //filepath,filename
                    $("#LawFileName").html(file.name);
                    $("#LawFilePath").val(jsonres.filepath);
                    $("#LawFileName").attr("href", jsonres.filepath)
                    $("#fileQueue").hide();
                },
                'onUploadError': function (file, errorCode, errorMsg, errorString) {
                    var msgText = "上传失败\n";
                    switch (errorCode) {
                    case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                        msgText += "HTTP 错误\n" + errorMsg;
                        break;
                    case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                        msgText += "上传文件丢失,请重新上传";
                        break;
                    case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                        msgText += "IO错误";
                        break;
                    case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                        msgText += "安全性错误\n" + errorMsg;
                        break;
                    case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                        msgText += "每次最多上传 " + this.settings.uploadLimit + "个";
                        break;
                    case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
                        msgText += errorMsg;
                        break;
                    case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
                        msgText += "找不到指定文件,请重新操作";
                        break;
                    case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                        msgText += "参数错误";
                        break;
                    default:
                        msgText += "文件:" + file.name + "\n错误码:" + errorCode + "\n"
                            + errorMsg + "\n" + errorString;
                    }
                    iTools.showError("文件:" + file.name + msgText);
                }
            });

        });
    </script>

C#代码

在Global.asax中添加如下代码

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            try
            {
                string session_param_name = "ASPSESSID";
                string session_cookie_name = "ASP.NET_SESSIONID";

                if (HttpContext.Current.Request.Form[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
                }
            }
            catch (Exception)
            {
            }

            try
            {
                string auth_param_name = "AUTHID";
                string auth_cookie_name = FormsAuthentication.FormsCookieName;

                if (HttpContext.Current.Request.Form[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
                }

            }
            catch (Exception)
            {
            }


        }
        void UpdateCookie(string cookie_name, string cookie_value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
            if (cookie == null)
            {
                HttpCookie cookie1 = new HttpCookie(cookie_name, cookie_value);
                Response.Cookies.Add(cookie1);
            }
            else
            {
                cookie.Value = cookie_value;
                HttpContext.Current.Request.Cookies.Set(cookie);
            }
        }