3

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?

David Cain
  • 16,484
  • 14
  • 65
  • 75
Farsen
  • 1,387
  • 3
  • 21
  • 48

1 Answers1

4

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
    });
});
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks alot for your help. Ive tried to add the generic handler you suggested, and changed the endpoint to the handlers file name, but it still doesent work. When i debug with Firebug, I now get the error: [FineUploader] Processing 1 files or inputs... [FineUploader] Sending upload request for 0 [FineUploader] xhr - server response received for 0 HTTP Error 404 - Not Found. So it seems it cannot find the handler.. – Farsen Jan 15 '13 at 10:43
  • 1
    Make sure that the name of the handler is correct. Also if you have a virtual directory name in the url make sure you have specified it. You could use the `ResolveUrl` method to set the path to the handler in a correct way: `endpoint: '<%= ResolveUrl("~/uploadhandler.ashx") %>'`. – Darin Dimitrov Jan 15 '13 at 10:46