1

我已经看了好几天了,但没有成功。我正在尝试为网站创建一个多文件上传系统。我尝试了几个 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 错误而失败,并且再次没有任何保存。感谢您的任何帮助。

4

1 回答 1

2

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

将以下片段之一添加到 web.config

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

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

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

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

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

于 2011-12-14T21:54:19.487 回答