使用.ashx进行多个文件上传

时间:2011-12-07 17:39:01

标签: file-upload upload uploadify swfupload image-upload

我一直在看这几天但没有成功。我正在尝试为网站创建一个多文件上传系统。我尝试了几个jQuery插件,包括SWFUpload和Uploadify。一切都有效,除了实际上传;上传的文件永远不会保存到文件夹中。我猜它是我使用.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错误而失败,并且再次没有任何保存。感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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

将以下其中一个代码段添加到web.config

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

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

如果您只想允许访问新处理程序,请执行此操作

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

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

相关问题