我正在使用uploadify上传我的文件,我想保存文件,我想在数据库中保存路径,所以我在会话中和用户提交表单后保存路径。它可以在 Internet Explorer 上运行,但在 Firefox 上由于会话 ID 的更改而无法运行。
如何解决这个问题呢?
我正在使用uploadify上传我的文件,我想保存文件,我想在数据库中保存路径,所以我在会话中和用户提交表单后保存路径。它可以在 Internet Explorer 上运行,但在 Firefox 上由于会话 ID 的更改而无法运行。
如何解决这个问题呢?
uploadify 插件不发送 cookie,因此服务器无法识别会话。实现此目的的一种可能方法是使用scriptData
参数将 sessionId 包含为请求参数:
<script type="text/javascript">
$(function () {
$('#file').uploadify({
uploader: '<%= Url.Content("~/Scripts/jquery.uploadify-v2.1.4/uploadify.swf") %>',
script: '<%= Url.Action("Index") %>',
folder: '/uploads',
scriptData: { ASPSESSID: '<%= Session.SessionID %>' },
auto: true
});
});
</script>
<% using (Html.BeginForm()) { %>
<input id="file" name="file" type="file" />
<input type="submit" value="Upload" />
<% } %>
这会将 ASPSESSID 参数与文件一起添加到请求中。接下来我们需要在服务器上重建会话。这可以在以下Application_BeginRequest
方法中完成Global.asax
:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string sessionParamName = "ASPSESSID";
string sessionCookieName = "ASP.NET_SessionId";
if (HttpContext.Current.Request[sessionParamName] != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[sessionCookieName];
if (null == cookie)
{
cookie = new HttpCookie(sessionCookieName);
}
cookie.Value = HttpContext.Current.Request[sessionParamName];
HttpContext.Current.Request.Cookies.Set(cookie);
}
}
最后,将接收上传的控制器操作可以使用会话:
[HttpPost]
public ActionResult Index(HttpPostedFileBase fileData)
{
// You could use the session here
var foo = Session["foo"] as string;
return View();
}