如何将 ASP.NET 处理程序的 url 传递给 jquery 文件上传?

How to pass url of ASP.NET handler to jquery fileupload?

我使用 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 (Internal Server Error)

.

-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)

您需要在 web.config 文件中 register your HttpHandler,例如:

<configuration>
  <system.webServer>
    <handlers>
      <add name="AjaxFileHandler" verb="*" 
        path="AjaxFileHandler.ashx" 
        type="UCTS.Board.AjaxFileHandler" />
    </handlers>
  </system.webServer>
</configuration>

注意:路径属性的值定义了要用于调用处理程序的 URL。在上面的示例中,它将是 url: 'AjaxFileHandler.ashx'.

我发现 .ashx 文件

顶部缺少有关处理程序的初始化

在 AjaxFileHandler.ashx 中添加了以下句子并正在努力。

<%@ WebHandler Language="C#" Class="AjaxFileHandler" %>

<%@ WebHandler Language="C#" Class="AjaxFileHandler" %>

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

public class AjaxFileHandler : IHttpHandler
{
    #region IHttpHandler Members

    public bool IsReusable
    {
        // Return false in case your Managed Handler cannot be reused for another request.
        // Usually this would be false in case you have some state information preserved per request.
        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
}