C# 使用jQuery和Asp.Net上载文件时出错

C# 使用jQuery和Asp.Net上载文件时出错,c#,jquery,asp.net,C#,Jquery,Asp.net,当我尝试将文件上载到服务器时,mscorlib.dll中首次出现“System.ArgumentOutOfRangeException”类型的异常 $('#But_Upload').click(function () { console.log("its working\n"); var ajaxRequest = $.ajax({ type: "POST", url: "../HAN

当我尝试将文件上载到服务器时,mscorlib.dll中首次出现“System.ArgumentOutOfRangeException”类型的异常

$('#But_Upload').click(function () {
            console.log("its working\n");
            var ajaxRequest = $.ajax({
                type: "POST",
                url: "../HANDLERS/Handler1.ashx",
                contentType: false,
                processData: false,
                data: $('#upload').get(0).files.toString(),
                async: true,
                success: function (xhr) {
                    console.log("done");
                }
            });
        });
    });
</script>
我在httpPostedFile行中得到了一个异常,我一直在努力解决这个问题,请帮助。

使用这个

public void ProcessRequest(HttpContext context)
    {
        HttpPostedFile postedFile = context.Request.Files[0];
        string savepath = "", tempPath = "";
        context.Response.ContentType = "text/plain";
        tempPath = System.Configuration.ConfigurationManager.AppSettings["YourFilePath"];
        savepath = context.Server.MapPath(tempPath);
        string extension = System.IO.Path.GetExtension(postedFile.FileName);
        string filename = System.IO.Path.GetFileNameWithoutExtension(postedFile.FileName) + extension;
        if (!Directory.Exists(savepath))
        {
            Directory.CreateDirectory(savepath);
        }
        System.IO.File.Delete(savepath + @"\" + filename);
        postedFile.SaveAs(savepath + @"\" + filename);
    }
关于脚本:

$('#But_Upload').click(function () {
    var files = $("#<%=upload.ClientID %>").get(0).files;
    var test = new FormData();
    test.append(files[0].name, files[0]);
    console.log("its working\n");
    var ajaxRequest = $.ajax({
        type: "POST",
        url: "../HANDLERS/Handler1.ashx",
        contentType: false,
        processData: false,
        data: test,
        async: true,
        success: function (xhr) {
            console.log("done");
        }
    });
});
$('But#Upload')。单击(函数(){
var files=$(“#”).get(0).files;
var测试=新FormData();
test.append(文件[0]。名称,文件[0]);
console.log(“其工作\n”);
var ajaxRequest=$.ajax({
类型:“POST”,
url:“../HANDLERS/Handler1.ashx”,
contentType:false,
processData:false,
数据:测试,
async:true,
成功:功能(xhr){
控制台日志(“完成”);
}
});
});

试试这段代码。IE在这里有一个特殊的变化,因此它适用于所有浏览器。我猜你只在IE中面对这个问题

HttpPostedFile hpf = TryCast(Context.Request.Files(0), HttpPostedFile);

if ((HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")) {
    string[] files = hpf.FileName.Split(new char[] {"\\", c});
    MySerializedobject.FileName = files[(files.Length - 1)];
}
else {
    MySerializedobject.FileName = hpf.FileName;
}

string FileType = hpf.ContentType;
string FileExtension = Path.GetExtension(hpf.FileName);
MySerializedobject.FileType = ("." + MySerializedobject.FileName.Split(".")[1]);
MySerializedobject.FileSize = hpf.ContentLength;
MySerializedobject.FileStream = hpf.InputStream;

你能试试这个答案吗?@shrihariyer-永远不要说你收到了什么异常/错误消息-我们喜欢猜测。@onsjjss我正在尝试上传一个图像。有其他链接吗?添加了解决方案。请立即尝试。获取异常System.ArgumentOutOfRangeException{“索引超出范围。必须为非负且小于集合的大小。\r\n参数名称:索引“}请提供帮助
HttpPostedFile hpf = TryCast(Context.Request.Files(0), HttpPostedFile);

if ((HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")) {
    string[] files = hpf.FileName.Split(new char[] {"\\", c});
    MySerializedobject.FileName = files[(files.Length - 1)];
}
else {
    MySerializedobject.FileName = hpf.FileName;
}

string FileType = hpf.ContentType;
string FileExtension = Path.GetExtension(hpf.FileName);
MySerializedobject.FileType = ("." + MySerializedobject.FileName.Split(".")[1]);
MySerializedobject.FileSize = hpf.ContentLength;
MySerializedobject.FileStream = hpf.InputStream;