如何在 ajax 中使用 Url 调用 handler.ashx

How to use Url in ajax to call a handler.ashx

本文关键字:Url 调用 handler ashx ajax      更新时间:2023-09-26

嘿,我的项目的目录是这样的:

project 
  codes
    fileupload(ashx)
  scripts
  image
  web-pages
    home(aspx)
    about(aspx)
    FileUpload(aspx)

我在我的文件上传(aspx)中使用了这个ajax函数:

<script>
function ajaxFileUpload() {
    $.ajaxFileUpload({
        url: "/FileUpload.ashx", // The problem is here
        secureuri: false,
        fileElementId: 'fileToUpload',
        dataType: 'json',
        success: function (data, status) {
            if (typeof (data.error) != 'undefined') {
                if (data.error != '') {
                    alert(data.error);
                } else {
                    ShowUploadedFiles(data.upfile, filename);
                    $('#fileToUpload').val("");
                }
            }
        },
        error: function (data, status, e) {
            alert(e);
        }
    });
}
</script>

如何从这个函数调用我的处理程序(fileupload.ashx):我尝试了很多方法:

1 - url: "../codes/FileUpload.ashx", Not working 
2 - url: "~/codes/FileUpload.ashx", Not working
3 - url: "./codes/FileUpload.ashx", Not working 
4 - url: "/codes/FileUpload.ashx", Not working
5 - url: "/project/codes/FileUpload.ashx", not working 
6 - url: "codes/FileUpload.ashx", not working 
7 - url: "/FileUpload.ashx", not working 

PS :当我更改文件上传的目录时.aspx并将其放在项目的主文件夹中...它将与这个网址完美配合:codes/FileUpload.ashx

那么有什么想法吗?

如果你在 IIS 中托管,并且你的 Web 应用的根目录设置为/project/web-pages/目录,则需要在该目录中拥有 Web 应用的所有其他目录。即:

如果工作 URL 结构使 http://your.url/home.aspx 正常工作,那么您的项目将需要按以下方式构建:

/project/web-pages/
/project/web-pages/image
/project/web-pages/scripts
/project/web-pages/codes

否则,IIS 不会提供/project/codes 中的文件。除非为/codes 设置单独的 IIS 虚拟目录。

如果 IIS 配置为您的 URL 看起来像 http://your.url/web-pages/home.aspx 那么应该可以使用/codes/fileupload.ashx 相对路径访问/codes 目录,前提是您已将网站的根目录配置为/project/目录。