c# - 使用 jquery 和处理程序 (ashx) 上传文件时出现“未找到”错误

标签 c# asp.net asp.net-ajax

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);
        }
    });
}

注意:我的 Aspx 页面是不同的文件夹,Image 文件夹和 UploadHandler.ashx.cs 是路由文件夹错误吗?

每次运行 ajax 请求 后都会出现 Not-Found 错误,如何修复。

谢谢。

最佳答案

你没有提到你使用的是哪个上传控件,我假设它是一个服务器端,你需要按如下方式访问它

改变

$('#f_UploadImage')

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

关于c# - 使用 jquery 和处理程序 (ashx) 上传文件时出现“未找到”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118200/

相关文章:

c# - 如何从用户控件 View 模型中关闭窗口?

asp.net - .net 4.6框架已就地升级那么.net框架4.5在vs2015中如何工作?

asp.net - Visual Studio-阻止停止关闭Internet Explorer的调试

c# - HtmlControl 更改类

asp.net - ItemTemplate 内的 UpdatePanel 刷新整个网格

javascript - 如何模仿 ASP.NET AjaxOptions 委托(delegate)函数?

c# - 我如何在 ASP.NET MVC 中进行分页?

c# - 这是什么意思?

c# - 返回语句应该在锁内还是锁外?

javascript - 如何使用Razor显示模板将字符串转换为html字符串