编程语言
首页 > 编程语言> > c#-如何在asp.net应用程序中从jquery设置HttpPost终结点

c#-如何在asp.net应用程序中从jquery设置HttpPost终结点

作者:互联网

我正在尝试使用Fine Uploader jQuery示例.基本上,这只是将文件从客户端页面上传到Web服务器的一种方法.

有一个示例,说明了如何在project’s GitHub page的服务器端设置文件流.

如何从脚本中调用此HttpPost端点?简单的jQuery示例如下所示:

$(document).ready(function () {
      $('#jquery-wrapped-fine-uploader').fineUploader({
        request: {
          endpoint: 'WhatToWriteHere??'
        },
        debug: true
      });
    });

那么您在端点中键入什么呢?我想它会像Namespace.Namespace.ClassName.UploadMethod()一样,但是我一直在和它打交道,但是无法使其正常工作.使用Firebug进行调试时,出现以下错误:

405 Method Not Allowed  
[FineUploader] xhr - server response received for 0
The HTTP verb POST used to access path '/FineUploaderTest/Uploadfolder' is not allowed.

任何想法?

解决方法:

您可以编写一个generic HttpHandler来处理文件上传:

public class UploadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var request = context.Request;
        var formUpload = request.Files.Count > 0;

        var xFileName = request.Headers["X-File-Name"];
        var qqFile = request["qqfile"];
        var formFilename = formUpload ? request.Files[0].FileName : null;

        var filename = xFileName ?? qqFile ?? formFilename;
        var inputStream = formUpload ? request.Files[0].InputStream : request.InputStream;

        var filePath = Path.Combine(context.Server.MapPath("~/App_Data"), filename);
        using (var fileStream = File.OpenWrite(filePath))
        {
            inputStream.CopyTo(fileStream);
        }

        context.Response.ContentType = "application/json";
        context.Response.Write("{\"success\":true}");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

接着:

$(document).ready(function () {
    $('#jquery-wrapped-fine-uploader').fineUploader({
        request: {
            endpoint: '/uploadhandler.ashx'
        },
        debug: true
    });
});

标签:http-post,asp-net,c,jquery
来源: https://codeday.me/bug/20191031/1975396.html