【问题标题】:How to pass url of ASP.NET handler to jquery fileupload?如何将 ASP.NET 处理程序的 url 传递给 jquery 文件上传?
【发布时间】:2016-03-11 12:01:37
【问题描述】:

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

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

想知道AjaxFileHandler的url怎么正确写?

我尝试让 ASP.NET Handler "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
}

【问题讨论】:

  • 打印屏幕的可读性不是很好。请改为在问题中输入代码。
  • @PavelV.) 哎呀.. 我编辑了我的帖子。

标签: javascript jquery asp.net jquery-file-upload


【解决方案1】:

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

您收到错误是因为您的项目中不存在这样的 url(http://localhost:5468/AjaxFileHandler)

【讨论】:

  • 也出现了错误POST http://localhost:5468/AjaxFileHandler.ashx 500 (Internal Server Error)
  • 你能调试ashx文件中的代码吗?或者您能否提供有关错误的更多详细信息(错误代码 500 可以是与服务器相关的任何内容)。如果您使用的是 IE,您可能会通过执行此操作获得更多错误详细信息 - 转到“Internet 选项”,然后转到“高级”,然后取消选中“显示友好错误消息”选项。
  • 我使用 chrome。这是我制作的测试代码压缩文件Google Drive
【解决方案2】:

我发现 .ashx 文件顶部缺少有关处理程序的初始化

在 AjaxFileHandler.ashx 中添加了下面的语句,正在处理中。

<%@ 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
}

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

    • 我尝试了您的建议,但没有奏效我可以看到错误POST http://localhost:5468/AjaxFileHandler.ashx 500 (Internal Server Error),我使用的是 chrome。这是我的测试代码压缩文件Google Drive
    • @NathanielJobs:在ProcessRequest() 方法中放置一个断点,然后启动调试器。然后从网络浏览器调用localhost:5468/AjaxFileHandler.ashx。 Visual Studio 会在断点处停止吗?
    • 其实ProcessRequest()方法没有被调用...这个链接是Test screenshot Google Drive
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2010-11-02
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多