Uploadify這個基於Flash的支持多檔上傳的Jquery外掛程式
官網:http://www.uploadify.com/
問題:
uploadify在ie下可以正常上傳,在實現非同步上傳的時候,每一個檔在上傳時都會提交給伺服器一個請求。每個請求都需要安全驗證,session和cookie的校驗。由於uploadify是借助flash來上傳的,每一次向後臺發送資料流程請求時,ie會自動把本地cookie存儲捆綁在一起發送給伺服器。但firefox、chrome不會這樣做,所以在firefox 和chrome會出現http error 和 IO error.
解決方法:
在Global.asax加上:
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
{
}
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 {
}
}
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);//重新設定請求中的cookie值,將伺服器端的session值賦值給它
}
<script type = "text/javascript">
var
auth = "<% =
Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty :
Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
var
ASPSESSID = "<%= Session.SessionID %>";
$(window).load(
function ()
{
$("#<%=FileUpload1.ClientID%>").fileUpload({
'uploader':
'/js/uploader.swf',
'cancelImg':
'/image/cancel.png',
'buttonText':
'Browse Files',
'script':
'/js/Upload.ashx',
'folder':
'uploads',
'fileDesc':
'Image Files',
'fileExt':
'*.jpg;*.jpeg;',
'multi':
true,
'auto':
false,
'simUploadLimit'
: 1, //如果沒限制有幾會上傳不完整
'onComplete':
function (event, ID, fileObj, response, data) {
$("#msg").append(response
+ "<br/>");
},
'onError':
function (event, ID, fileObj, errorObj) {
alert(errorObj.type + ' Error: ' + errorObj.info + errorObj.status +
errorObj.text);
},
'scriptData':
{ 'ASPSESSID': ASPSESSID,'AUTHID':
auth }
});
}
);
function
cleanImg() {
$("#msg").html("");
}
</script>
<%@ WebHandler Language="C#" Class="Upload" %>
using System;
using System.Web;
using System.IO;
public class Upload : IHttpHandler
{
public void ProcessRequest (HttpContext
context) {
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
try
{
HttpPostedFile
postedFile = context.Request.Files["Filedata"];
// string
savepath = "";
string
tempPath = "";
tempPath = @"c:\temp\";
//savepath
= context.Server.MapPath(tempPath);
string
filename = postedFile.FileName;
if
(!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath);
string
file2 = tempPath + filename;
int
count = 1;
//if
exist, filename "-" +1
while
(File.Exists(file2))
{
file2 = tempPath + filename + "-" + count + ".JPG";
count++;
}
postedFile.SaveAs(file2);
//context.Response.Write(file2.Replace("c:",""));
context.Response.Write(context.Request["firstName"]);
context.Response.StatusCode = 200;
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}
public bool IsReusable {
get {
return
false;
}
}
}
沒有留言:
張貼留言