1

I use JQuery file upload, and ASP.NET 4.0 Web Application project.

but I don't know how to pass my ASP.NET C# handler url...

I want to know how to correctly write the url of AjaxFileHandler?

I tried that make ASP.NET Handler "AjaxFileHandler.ashx" and "url: AjaxFileHandler.ashx" but a error appears

POST http://localhost:5468/AjaxFileHandler.ashx 500 (Internal Server Error)

.

-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
}
2
  • The printscreen is not very readable. Please type the code into the question instead. Commented Mar 11, 2016 at 12:06
  • @PavelV.) Oops.. I edited my post. Commented Mar 11, 2016 at 12:19

3 Answers 3

1

You can create AjaxFileHandler.ashx handler in your project and call the url like url:"AjaxFileHandler.ashx"

You are getting the error because there no such url(http://localhost:5468/AjaxFileHandler) exists in your project

Sign up to request clarification or add additional context in comments.

3 Comments

It's also appear a error POST http://localhost:5468/AjaxFileHandler.ashx 500 (Internal Server Error)
Are you able to debug the code in ashx file? or can you provide more detail on error (error code 500 can be anything related to server) . If you are using IE you may get more details of error by doing this - Goto "Internet Options", then "Advanced", and uncheck the option for "Show Friendly Error Messages".
I use chrome. This is I made test code zip file Google Drive
1

I find missing initializing about handler on the top of the .ashx file

Added below sentence in AjaxFileHandler.ashx and is working on it.

<%@ WebHandler Language="C#" Class="AjaxFileHandler" %>

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

Comments

0

You need to register your HttpHandler in the web.config file, e.g:

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

Note: the value of the path attribute defines the URL to be used to invoke the handler. In the above sample it would be url: 'AjaxFileHandler.ashx'.

3 Comments

I try your advise but didn't work I can see a error POST http://localhost:5468/AjaxFileHandler.ashx 500 (Internal Server Error), I use chrome. This is my test code zip file Google Drive
@NathanielJobs: put a breakpoint in the ProcessRequest() method, and start the debugger. Then call localhost:5468/AjaxFileHandler.ashx from a web browser. Does visual studio stop at the breakpoint?
Actually ProcessRequest() method doesn't called... This link is Test screenshot Google Drive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.