【问题标题】:'Not found' error in upload file using jquery and handler(ashx)使用 jquery 和处理程序(ashx)上传文件时出现“未找到”错误
【发布时间】:2016-12-13 09:59:38
【问题描述】:

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 and UploadHandler.ashx.cs 是路由文件夹错误吗?

每次运行ajax request 后,它都会给出Not-Found 错误,如何解决。

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    改变

    $('#f_UploadImage')
    

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

    【讨论】:

      【解决方案2】:

      如你所说

      我的 Aspx 页面是不同的文件夹和 Image Folder 和 UploadHandler.ashx.cs

      你必须改变

      url: 'UploadHandler.ashx',
      

      url: '/UploadHandler.ashx',
      

      否则它将尝试在与ajax页面相同的文件夹中搜索UploadHandler.ashx并给出404。

      【讨论】:

      • 响应选项卡中有详细信息吗?
      【解决方案3】:

      我认为问题在于 contentType 尝试

      contentType: 'multipart/form-data',

      【讨论】:

        【解决方案4】:

        感谢您的所有宝贵反馈,

        现在我的问题已经解决了, UploadHandler.ashx 设置中的问题

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

        继承值与我的 UploadHandler.ashx.cs 命名空间不匹配,这是问题所在,现在已修复。

        谢谢大家。

        【讨论】:

          猜你喜欢
          • 2012-01-18
          • 2013-06-02
          • 1970-01-01
          • 2011-07-02
          • 2015-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多