【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

时间:2023-03-10 08:30:51
【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

一. 关于Uploadify

Uploadify是一个jQuery插件,你可以很容易的为你的网站添加多个文件上传功能。有两个不同的版本(HTML5和Flash)允许你灵活选择为您的网站和回退方法正确实施使其降解优雅。

二. 问题整理

问题1:页面报插件的图片找不到

  遇到这个问题,很明显是相关文件中引用的路径不正确。

    找到下边这个文件,修改相关路径为正确的路径。

【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

问题2:页面报net::ERR_NAME_NOT_RESOLVED

  找到下边这个文件

【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

在插件主文件查找

this.settings.button_image_url=SWFUpload.completeURL(this.settings.button_image_url);

将其改为:

if(this.settings.button_image_url!=""){
this.settings.button_image_url=SWFUpload.completeURL(this.settings.button_image_url);
}

注意:此处要粘贴复制不要使用vs的自带替换功能,避免将其他代码错误替换。

问题3:服务器传session

首先在Global.asax配置文件下添加两个获取session方法

方法1:

 protected void Application_BeginRequest(object sender, EventArgs e)
{
/* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
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
{
} 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
{
}
}

方法2:

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

在使用的界面添加取值操作,要添加在调用插件的上面(这里使用C#中的 @符号在页面中获取,并作为JS代码中的全局变量)

 var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
var ASPSESSID = "@Session.SessionID";

然后在插件的formData里面写入:'folder': '/Upload', 'ASPSESSID': ASPSESSID, 'AUTHID': auth

整个效果如下图所示:

【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案

【原创】MVC项目中使用JQuery的upladify图片上传插件相关问题的解决方案