我使用Uploadify上傳與ASP.NET一些圖像不確定的。
我在ASP.NET中使用Response.WriteFile()
將上傳結果返回給JavaScript。 正如文檔中所述,我使用onAllComplete
事件來檢查來自ASP.NET的響應字符串。Uploadify響應值總是從ASP.NET
的問題是它的alert(response);
在JavaScript中總是不確定的。
JavaScript代碼如下:
$(document).ready(function() {
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
$('#btnUpdateProfileImg').uploadify({
'uploader': '../script/uploadify/uploadify.swf',
'script': '../uploadprofimg.aspx',
'cancelImg': '../script/uploadify/cancel.png',
'folder': '../script/uploadify',
'scriptData': { 'id': $(this).attr("id"), 'token': auth },
'onAllComplete': function(event, queueID, fileObj, response, data) {
alert(response);
}
});
});
ASP.NET代碼低於;
protected void Page_Load(object sender, EventArgs e)
{
try
{
string token = Request.Form["token"].ToString();
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
if (ticket != null)
{
var identity = new FormsIdentity(ticket);
if (identity.IsAuthenticated)
{
HttpPostedFile hpFile = Request.Files["ProfileImage"];
string appPath = HttpContext.Current.Request.ApplicationPath;
string fullPath = HttpContext.Current.Request.MapPath(appPath) + @"\avatar\";
hpFile.SaveAs(Server.MapPath("~/" + uniqName));
Response.ContentType = "text/plain";
Response.Write("test");
}
}
}
catch (Exception ex)
{
Response.Write("test");
}
}
原因的FormsAuthenticationTicket
目的是使用與Firefox的Uploadify時雖然通過認證的cookie。
我見過許多例子Response.Write
返回一個值回onAllComplete
事件。但我所得到的是未定義的。 我也曾嘗試使用Context.Response.Write
,this.Response.Write
,HttpContext.Current.Response.Write
。他們都返回undefined。
任何幫助表示讚賞。 感謝
當您設置上,如果(identity.IsAuthenticated)的斷點被擊中斷點? – Tony 2011-03-04 17:46:23
我會添加一個'默認'響應.NET代碼,以確保正確的執行流程。您的調試還沒有排除所有選項。 – cusimar9 2011-03-04 18:09:32
是的,identity.IsAuthenticated返回true並保存圖像。還有Response.Write(「test」);線在.NET代碼存在之前實際上被命中。 – Sivakanesh 2011-03-04 18:19:21