ฉันมีปัญหาที่คล้ายกันเมื่อฉันพยายามใช้ uploadify กับ asp.net mvc3 หลังจาก googling มากนี่คือสิ่งที่ฉันพบว่าดูเหมือนว่าจะได้ผล โดยพื้นฐานแล้วเกี่ยวข้องกับการประกาศแอตทริบิวต์ที่กำหนดเอง จากนั้นส่งผ่านคุกกี้การตรวจสอบสิทธิ์ไปยังโพสต์ HTTP ของ Uploadify อย่างชัดเจน และประมวลผลคุกกี้ด้วยตนเองผ่านแอตทริบิวต์ที่กำหนดเอง
ก่อนอื่นให้ประกาศแอตทริบิวต์ที่กำหนดเองนี้:
/// <summary>
/// A custom version of the <see cref="AuthorizeAttribute"/> that supports working
/// around a cookie/session bug in Flash.
/// </summary>
/// <remarks>
/// Details of the bug and workaround can be found on this blog:
/// http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class TokenizedAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// The key to the authentication token that should be submitted somewhere in the request.
/// </summary>
private const string TOKEN_KEY = "authCookie";
/// <summary>
/// This changes the behavior of AuthorizeCore so that it will only authorize
/// users if a valid token is submitted with the request.
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
string token = httpContext.Request.Params[TOKEN_KEY];
if (token != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
if (ticket != null)
{
var identity = new FormsIdentity(ticket);
string[] roles = System.Web.Security.Roles.GetRolesForUser(identity.Name);
var principal = new GenericPrincipal(identity, roles);
httpContext.User = principal;
}
}
return base.AuthorizeCore(httpContext);
}
}
จากนั้นประกาศการดำเนินการนี้ให้ส่งคุกกี้การรับรองความถูกต้องไปยัง ViewData ของคุณ เพื่อให้คุณสามารถส่งผ่านไปยังวิดเจ็ต Uploadify นี่จะเป็นการดำเนินการที่คุณเรียกใช้เพื่อสร้างอินสแตนซ์วิดเจ็ต Uploadify ในภายหลัง
[Authorize]
public ActionResult UploadifyUploadPartial()
{
ViewBag.AuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName] == null
? string.Empty
: Request.Cookies[FormsAuthentication.FormsCookieName].Value;
return PartialView("UploadifyUpload");
}
นี่คือโค้ดสำหรับมุมมอง UploadifyUpload โดยพื้นฐานแล้วมันเป็น wrapper สำหรับ JavaScript ซึ่งตั้งค่า Uploadify ฉันคัดลอกของฉันโดยไม่มีการเปลี่ยนแปลง ดังนั้นคุณจะต้องปรับให้เข้ากับใบสมัครของคุณ สิ่งสำคัญที่ควรทราบคือคุณกำลังส่ง authCookie
ไปยังคุณสมบัติ scriptData
ผ่านทาง ViewData จากการดำเนินการ UploadifyUploadPartial
@if (false)
{
<script src="../../Scripts/jquery-1.5.1-vsdoc.js" type="text/javascript"></script>
}
@{
ViewBag.Title = "Uploadify";
}
<script src="@Url.Content("~/Scripts/plugins/uploadify/swfobject.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/plugins/uploadify/jquery.uploadify.v2.1.4.min.js")" type="text/javascript"></script>
<link href="/[email protected]("~/Scripts/plugins/uploadify/uploadify.css")" rel="stylesheet" type="text/css" />
<script>
$(document).ready(function () {
CreateUploadifyInstance("dir-0");
});
function CreateUploadifyInstance(destDirId) {
var uploader = "@Url.Content("~/Scripts/plugins/uploadify/uploadify.swf")";
var cancelImg = "@Url.Content("~/Scripts/plugins/uploadify/cancel.png")";
var uploadScript = "@Url.Content("~/Upload/UploadifyUpload")";
var authCookie = "@ViewBag.AuthCookie";
$('#uploadifyHiddenDummy').after('<div id="uploadifyFileUpload"></div>');
$("#uploadifyFileUpload").uploadify({
'uploader': uploader,
'cancelImg': cancelImg,
'displayData': 'percentage',
'buttonText': 'Select Session...',
'script': uploadScript,
'folder': '/uploads',
'fileDesc': 'SunEye Session Files',
'fileExt': '*.son2',
'scriptData' : {'destDirId':destDirId, 'authCookie': authCookie},
'multi': false,
'auto': true,
'onCancel': function(event, ID, fileObj, data) {
//alert('The upload of ' + ID + ' has been canceled!');
},
'onError': function(event, ID, fileObj, errorObj) {
alert(errorObj.type + ' Error: ' + errorObj.info);
},
'onAllComplete': function(event, data) {
$("#treeHost").jstree("refresh");
//alert(data.filesUploaded + ' ' + data.errors);
},
'onComplete': function(event, ID, fileObj, response, data) {
alert(ID + " " + response);
}
});
}
function DestroyUploadifyInstance() {
$("#uploadifyFileUpload").unbind("uploadifySelect");
swfobject.removeSWF('uploadifyFileUploadUploader');
$('#uploadifyFileUploadQueue').remove();
$('#uploadifyFileUploadUploader').remove();
$('#uploadifyFileUpload').remove();
}
</script>
<div id="uploadifyHiddenDummy" style="visibility:hidden"></div>
<div id="uploadifyFileUpload">
</div>
สำหรับการดำเนินการของคุณที่อัปโหลดโพสต์ ให้ใช้แอตทริบิวต์ TokenizedAuthorize
ใหม่แทนแอตทริบิวต์ Authorize
:
[HttpPost]
[TokenizedAuthorize]
public string UploadifyUpload(HttpPostedFileBase fileData)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Form["authCookie"]);
if (ticket != null)
{
var identity = new FormsIdentity(ticket);
if (!identity.IsAuthenticated)
{
return "Not Authenticated";
}
}
// Now parse fileData...
}
สุดท้าย หากต้องการใช้โค้ดที่เราเขียนไว้ ให้เรียกใช้ UploadifyUploadPartial Action ผ่านทาง Html.Action Helper บนมุมมองใดๆ ที่คุณต้องการโฮสต์วิดเจ็ต Uploadify:
@Html.Action("UploadifyUploadPartial", "YourUploadControllerName")
คุณควรจะไปที่จุดนี้ดี รหัสควรใช้งานได้ใน FF, Chrome และ IE 9 โปรดแจ้งให้เราทราบหากคุณมีปัญหา
person
Bojin Li
schedule
15.04.2012