9

I am trying to upload a file using jquery ajax with handler (c#). The problem is, when I call the handler I get

context.Request.File.Count=0

Here is the aspx code:

<!--aspx file code-->
<script language="javascript" type="text/javascript">
$().ready(function () 
{
    $('#save').click(function (e)
    {
        CalluploaderHandler();
    });
});

function CalluploaderHandler()
{
    $.ajax({
                type: "POST",
                url: "Services/UPloader.ashx",
                contentType: "application/json; charset=utf-8",
                success: OnComplete,
                error: OnFail
            });
    return false;
}

function OnComplete(result)
{
    alert('Success');
}

function OnFail(result)
{
    alert('Request failed');
}

</script>
    </head>
        <body>
            <form  enctype="multipart/form-data">
                <label for="file">
                Filename:</label>
                <input name="file" id="file" type="file">
                <input id="save" name="submit" value="Submit" type="submit">
            </form>
        </body>
    </html>    

The c# code handler:

/* handler*/
public void ProcessRequest(HttpContext context)
{
    string savedFileName = "";

    foreach (string file in context.Request.Files)
    {
        HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;
        if (hpf.ContentLength == 0)
            continue;
        // savedFileName = context.Server.MapPath(Path.GetFileName(hpf.FileName));
        // hpf.SaveAs(savedFileName);
    }
    context.Response.Write(savedFileName);
}
eeerahul
  • 1,629
  • 4
  • 27
  • 38
user1092626
  • 101
  • 1
  • 1
  • 4

3 Answers3

3

You can add this kind of code to the handler file. Then you can post to this url(whateverroot/yourhandler.ashx)

The content type should be "multipart/form-data". For eg: If you're using a HTML form tag, then enctype="multipart/form-data".

public void ProcessRequest(HttpContext context)
{
        var result = "0";
        try
        {
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile file = null;

                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    file = context.Request.Files[i];
                    if (file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(<somepath>, fileName);
                        file.SaveAs(path);
                        result = "1"; 
                    }
                }    

            }
        }
        catch { }
      context.Response.Write(result);
}
Venugopal M
  • 2,280
  • 1
  • 20
  • 30
3

i think the problem is with the contentType try

contentType: 'multipart/form-data',

OR

contentType :'application/octet-stream';

see this post for more information

Sending multipart/formdata with jQuery.ajax

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101
2

Your code...

$.ajax({
    type: "POST",
    url: "Services/UPloader.ashx",
    contentType: "application/json; charset=utf-8",
    success: OnComplete,
    error: OnFail
});

..is missing the data parameter. The way it's currently written, nothing is being sent to the handler.

You need to pass the file to the handler, using the data parameter. Please have a go through this link: http://www.aspdotnet-suresh.com/2015/02/jquery-upload-images-files-without-page-refresh-postaback-in-aspnet.html

nhahtdh
  • 55,989
  • 15
  • 126
  • 162