Error "Not found" when downloading a file using jquery and handler (ashx)

UploadHandler.ashx.cs

public class UploadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        try
        {
            string dirFullPath = HttpContext.Current.Server.MapPath("~/Uploader/");
            string[] files;
            int numFiles;
            files = System.IO.Directory.GetFiles(dirFullPath);
            numFiles = files.Length;
            numFiles = numFiles + 1;
            string str_image = "";

            foreach (string s in context.Request.Files)
            {
                HttpPostedFile file = context.Request.Files[s];
                string fileName = file.FileName;
                string fileExtension = file.ContentType;

                if (!string.IsNullOrEmpty(fileName))
                {
                    fileExtension = Path.GetExtension(fileName);
                    str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
                    string pathToSave_100 = HttpContext.Current.Server.MapPath("~/Uploader/") + str_image;
                    file.SaveAs(pathToSave_100);
                }
            }
            //  database record update logic here  ()

            context.Response.Write(str_image);
        }
        catch (Exception ac)
        {

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

Jscode

/Image Upload code
function sendFile(file) {

    var formData = new FormData();
    formData.append('file', $('#f_UploadImage')[0].files[0]);

    $.ajax({
        url: 'UploadHandler.ashx',
        type: 'POST',
        data: formData,
        cache: false,
        processData: false,
        contentType: false,
        success: function(result) {
            if (result != 'error') {
                var my_path = "Uploader/" + result;
                $("#myUploadedImg").attr("src", my_path);
            }
        },
        error: function(err) {
            alert(err.statusText);
        }
    });
}


function callImgUploader() {
    var _URL = window.URL || window.webkitURL;
    $("#f_UploadImage").on('change', function() {

        var file, img;
        if ((file = this.files[0])) {
            img = new Image();
            img.onload = function() {
                sendFile(file);
            };
            img.onerror = function() {
                alert("Not a valid file:" + file.type);
            };
            img.src = _URL.createObjectURL(file);
        }
    });
}

Note. My Aspx page is a different folder, but Image Folder and UploadHandler.ashx.csis the wrong route folder?

after starting ajax requestevery time he gives an error Not-Found, how to fix it.

Thank.

+4
source share
4 answers

You did not indicate which control you are using, I assume that this is the server side, and you need to access it as follows

Edit

$('#f_UploadImage')

before

$('#<%= f_UploadImage.ClientID %>')
+1
source

As you said

My Aspx page is a different folder and a folder with images and UploadHandler.ashx.cs

You have to change

url: 'UploadHandler.ashx',

to

url: '/UploadHandler.ashx',

UploadHandler.ashx , ajax, 404.

0

, , contentType try

contentType: 'multipart/form-data',

0

,

Now my problem is fixed, the problem is in the configuration UploadHandler.ashx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadHandler.ashx.cs" Inherits="Customer.UploadHandler" %>

inherits the value, does not match my namespace UploadHandler.ashx.cs, that the problem is now fixed.

Thanks to everyone.

0
source

Source: https://habr.com/ru/post/1663602/


All Articles