Nathaniel Jobs
Nathaniel Jobs

Reputation: 162

How to pass url of ASP.NET handler to jquery fileupload?

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
}

Upvotes: 1

Views: 931

Answers (3)

Nathaniel Jobs
Nathaniel Jobs

Reputation: 162

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
}

Upvotes: 1

M4N
M4N

Reputation: 96576

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'.

Upvotes: 0

rinesh
rinesh

Reputation: 545

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

Upvotes: 1

Related Questions