我使用JQuery文件上传和ASP.NET 4.0 Web应用程序项目。

但是我不知道如何传递我的ASP.NET C#处理程序URL ...

我想知道如何正确编写AjaxFileHandler的URL?

我尝试使ASP.NET处理程序“ AjaxFileHandler.ashx”和“ URL:AjaxFileHandler.ashx”出现错误


  POST http://localhost:5468/AjaxFileHandler.ashx 500(内部服务器错误)




-Post.aspx-

<script type="text/javascript">
    $(function () {
        $('#fileupload').fileupload({
            datatype: "json",
            url: 'AjaxFileHandler.ashx',
            limitConcurrentUploads: 1,
            sequentialUpload: true,
            maxChunkSize: 100000,
            add: function (e, data) {
                $('#filelistholder').removeClass('hide');
                data.context = $('<div>').text(data.files[0].name).appendTo('#filelistholder');
                $('</div> \
                   <div class="progress"> \
                       <div class="progress-bar" style="width: 0%;"></div> \
                   </div>').appendTo(data.context);
                $('#btnUploadAll').click(function () {
                    data.submit();
                });
            },
            done: function (e, data) {
                data.context.text(data.files[0].name + ' (전송완료)');
                $('</div> \
                   <div class="progress"> \
                       <div class="progress-bar" style="width: 100%"></div> \
                   </div>').appendTo(data.context);
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#overallbar').css('width', progress + '%');
            },
            progress: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                data.context.find('.progress-bar').css('width', progress + '%');
            }
        });
    });

    function updateContent() {
        oEditors.getById["postContent"].exec("UPDATE_CONTENTS_FIELD", []);
    }
</script>


-AjaxFileHandler.ashx-

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

public class AjaxFileHandler : IHttpHandler
{
    #region IHttpHandler Members
    public bool IsReusable { get { return true; }}

    public void ProcessRequest(HttpContext context)
    {
        //write your handler implementation here.
        if (context.Request.Files.Count > 0)
        {
            string path = context.Server.MapPath("/UploadedFiles/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var file = context.Request.Files[0];
            string fileName = Path.Combine(path, file.FileName);
            file.SaveAs(fileName);
            context.Response.ContentType = "text/plain";
            context.Response.Write("<script>console.log('" + fileName + "');</script>");
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var result = new { name = file.FileName };
            context.Response.Write(serializer.Serialize(result));
        }
    }
    #endregion
}

最佳答案

您可以在项目中创建AjaxFileHandler.ashx处理程序,然后像url:"AjaxFileHandler.ashx"这样调用url

您收到错误,因为您的项目中不存在此类url(http://localhost:5468/AjaxFileHandler

10-06 11:53