c# – 使用jquery和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);
        }
    });
}

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

运行ajax请求后每次给出Not-Found错误怎么能修复它.

谢谢.

解决方法

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

更改

$('#f_UploadImage')

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...