2011-12-07 16 views
1

我一直在寻找这个好几天,但没有成功。我正在尝试为网站创建一个多文件上传系统。我已经尝试了几个jQuery插件,包括SWFUpload和Uploadify。除了实际上传以外,一切正常;上传的文件从不保存到文件夹。我猜测它是我使用.ashx的方式,所以对以下代码错误的任何指导表示赞赏。谢谢,使用.ashx多文件上传

//Jquery is inherited, and I'm pretty sure this is all correct 
<link href="/_uploadify/uploadify.css" type="text/css" rel="stylesheet" /> 
<script type="text/javascript" src="/_uploadify/swfobject.js"></script> 
<script type="text/javascript" src="/_uploadify/jquery.uploadify.v2.1.4.min.js"></script> 

//Again, this all works as expected. CSS displays, SWF works, the issue I THINK deals 
//with 'script' and 'folder'. My Upload.ashx is in the root, as well as uploads folder 
<script type="text/javascript"> 
$(document).ready(function() { 
    alert("here"); 
    $('#file_upload').uploadify({ 
     'uploader': '/_uploadify/uploadify.swf', 
     'script': 'Upload.ashx', 
     'cancelImg': '/_uploadify/cancel.png', 
     'folder': 'uploads', 
     'multi': true, 
     'auto': true 
    }); 
}); 
</script> 

<input id="file_upload" name="file_upload" type="file" /> 

//My handler code, I'm not sure if this works or not but I do know that if 
//I try to debug nothing ever fires... I'm sure that's part of the problem 
<%@ WebHandler Language="C#" Class="Upload" %> 

using System; 
using System.Web; 
using System.IO; 

public class Upload : IHttpHandler { 

public void ProcessRequest(HttpContext context) 
{ 
    HttpPostedFile file = context.Request.Files["Filedata"]; 

    file.SaveAs("C:\\" + file.FileName); 

    context.Response.ContentType = "text/plain"; 
    context.Response.Write("Hello World"); 
} 

public bool IsReusable 
{ 
    get 
    { 
     return false; 
    } 
} 
} 

这就是我拥有的一切。如果我尝试使用SWFUpload jQuery上传文件,它表示它成功,但没有任何文件保存到该文件夹​​。如果我尝试使用Uploadify,它会因HTTP错误或IO错误而失败,并且再次保存。感谢任何和所有的帮助。

+0

是您的ashx文件在会员区或其他?这可能是一个会话问题。 –

回答

2

经过一段时间和调试之后,我发现了它。问题实际上是使用表单身份验证,并在登录屏幕后的任何页面上发生。这里是修复...

添加下面的代码片段之一到web.config

如果您不担心任何身份验证尝试

<authorization> 
    <allow users="*"/> 
    </authorization> 

如果你只是想允许访问新的处理程序,做到这一点

<location path="_handlers/Upload.ashx"> 
<system.web> 
    <authorization> 
    <allow users="*"/> 
    </authorization> 
</system.web> 
</location> 

我这样做了调试点,我在我的处理程序文件后,击中和我能够从那里解决其余的问题。