简体   繁体   English

如何在asp.net应用程序中从jquery设置HttpPost端点

[英]How to set HttpPost endpoint from jquery, in asp.net application

I am trying to get an Fine Uploader jQuery example to work. 我正在尝试使用Fine Uploader jQuery示例。 Basically it is just a way to upload files from client page to the webserver. 基本上它只是一种从客户端页面上传文件到Web服务器的方法。

There is an example on how to set up the file stream on the server side on the project's GitHub page . 有一个关于如何在项目的GitHub页面上设置服务器端文件流的示例。

How do I from script call this HttpPost endpoint ? 如何从脚本调用此HttpPost endpoint The simple jQuery example looks like this: 简单的jQuery示例如下所示:

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

So what do you type in the endpoint ? 那你在endpoint什么? I suppose it would be something like Namespace.Namespace.ClassName.UploadMethod() , but I've been fiddling along time with it, but cant get it to work. 我想它会像Namespace.Namespace.ClassName.UploadMethod() ,但我一直在随便摆弄它,但不能让它工作。 When debugging with Firebug, I get the following error(s): 使用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.

Any idea? 任何的想法?

You could write a generic HttpHandler to handle the file upload: 您可以编写一个通用的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; }
    }
}

and then: 然后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM