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

走远了吗. 提交于 2019-12-10 18:13:30

问题


I am trying to get an Fine Uploader jQuery example to work. Basically it is just a way to upload files from client page to the webserver.

There is an example on how to set up the file stream on the server side on the project's GitHub page.

How do I from script call this HttpPost endpoint? The simple jQuery example looks like this:

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

So what do you type in the 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. When debugging with Firebug, I get the following error(s):

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?


回答1:


You could write a generic HttpHandler to handle the file upload:

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


来源:https://stackoverflow.com/questions/14333621/how-to-set-httppost-endpoint-from-jquery-in-asp-net-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!