我试图让Uploadify与我的站点一起工作,但我甚至在文件被发送到服务器之前就得到了一个通用的"HTTP错误“(我这么说是因为Fiddler没有向我的控制器显示任何post请求)。
我可以浏览正确的文件上传。队列中正确地填充了要上载的文件,但是当我点击submit按钮时,队列中的元素会出现红色,并表示HTTP错误。
总之,这是我的部分代码:
<% using ( Html.BeginForm( "Upload", "Document", FormMethod.Post, new { enctype = "multipart/form-data" } ) ) { %>
<link type="text/css" rel="Stylesheet" media="screen" href="/_assets/css/uploadify/uploadify.css" />
<script type="text/javascript" src="/_assets/js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="/_assets/js/uploadify/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("[ID$=uploadTabs]").tabs();
var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
$('#fileInput').uploadify({
uploader: '/_assets/swf/uploadify.swf',
script: '/Document/Upload',
folder: '/_uploads',
cancelImg: '/_assets/images/cancel.png',
auto: false,
multi: false,
scriptData: { token: auth },
fileDesc: 'Any document type',
fileExt: '*.doc;*.docx;*.xls;*.xlsx;*.pdf',
sizeLimit: 5000000,
scriptAccess: 'always', //testing locally. comment before deploy
buttonText: 'Browse...'
});
$("#btnSave").button().click(function(event) {
event.preventDefault();
$('#fileInput').uploadifyUpload();
});
});
</script>
<div id="uploadTabs">
<ul>
<li><a href="#u-tabs-1">Upload file</a></li>
</ul>
<div id="u-tabs-1">
<div>
<input id="fileInput" name="fileInput" type="file" />
</div>
<div style="text-align:right;padding:20px 0px 0px 0px;">
<input type="submit" id="btnSave" value="Upload file" />
</div>
</div>
</div>
<% } %>非常感谢你的帮助!
更新
我在上传脚本中添加了一个"onError“处理程序,以探索发生了哪些错误,如下所示
onError: function(event, queueID, fileObj, errorObj) {
alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
}并发现info属性包含302。我还添加了“方法”参数来上传'post'的值。
我正在包括我的控制器操作代码,以获取信息。我读过很多关于罗拉迪的文章,似乎我可以用下面的签名来操作.
[HttpPost]
public ActionResult Upload(string token, HttpPostedFileBase fileData) {
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
if (ticket!=null) {
var identity = new FormsIdentity(ticket);
if(identity.IsAuthenticated) {
try {
//Save file and other code removed
return Content( "File uploaded successfully!" );
}
catch ( Exception ex ) {
return Content( "Error uploading file: " + ex.Message );
}
}
}
throw new InvalidOperationException("The user is not authenticated.");
}有人能提供帮助吗?
发布于 2010-09-28 18:55:14
干得好,问题就解决了!
我的代码没有“正确”的问题。插件的使用通常是正确的,但身份验证机制存在问题。
每个人都可以在互联网上发现,闪存插件不与服务器端代码共享身份验证Cookie,这也是在我的代码中使用包含身份验证cookie的"scriptData“部分的原因。
这个问题与控制器被授权属性修饰有关,而这永远不会让请求到达其目的地。
在上传论坛上的另一个用户的帮助下,解决方案是编写一个自定义的AuthorizeAttribute版本,就像您在下面的代码中看到的那样。
/// <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 = "AuthenticationToken";
/// <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( System.Web.HttpContextBase httpContext ) {
string token = httpContext.Request.Params[TOKEN_KEY];
if ( token != null ) {
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt( token );
if ( ticket != null ) {
FormsIdentity identity = new FormsIdentity( ticket );
string[] roles = System.Web.Security.Roles.GetRolesForUser( identity.Name );
GenericPrincipal principal = new GenericPrincipal( identity, roles );
httpContext.User = principal;
}
}
return base.AuthorizeCore( httpContext );
}
}使用它来装饰完成上传的控制器/动作,使所有事情都能顺利地工作。
唯一未解决但不影响代码执行的奇怪之处是,Fiddler没有显示HTTP。我不明白为什么..。
我张贴这篇文章是为了向社会提供这一信息。
谢谢!
https://stackoverflow.com/questions/3791724
复制相似问题