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: "../HANDLERS/Handler1.ashx",
                contentType: false,
                processData: false,
                data: $('#upload').get(0).files.toString(),
                async: true,
                success: function (xhr) {
                    console.log("done");
                }
            });
        });
    });
</script>

我在 C# 处理程序中的服务器代码是

public void ProcessRequest(HttpContext context)
    {
        System.Diagnostics.Debug.Write(context.ToString());
        try
        {
                HttpPostedFile httpPostedFile = context.Request.Files.Get(0);
                var fileSavePath = HttpContext.Current.Server.MapPath("~/upl") + httpPostedFile.FileName;
                httpPostedFile.SaveAs(fileSavePath);
           }
        catch(Exception E)
        {  }
     }

我在 httpPostedFile 行得到了异常,我一直在与这个问题作斗争,请帮忙。

最佳答案

试试这个代码。这里有一个针对 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;

关于c# - 使用 jQuery 和 Asp.Net 上传文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300387/

相关文章:

c# - 如何使用 axmediaplayer 播放播放列表中的下一个项目?

c# - 通过 HTTP 处理程序连接到 SQL Server Express 数据库

javascript - 部分字符串搜索 javascript 对象数组以查找针

jquery - 将选项添加到动态加载的选择中

javascript - 如何用jquery切换改变html?

mysql - 在 SQL 查询中同时使用 "update"和 "join"来增加数据库值

c# - 在 .NET 远程处理中,RemotingConfiguration.RegisterWellKnownServiceType 和 RemotingServices.Marshal 之间有什么区别?

asp.net - 帮助设计 asp :button control using the Sliding Door technique

asp.net - session 状态阻止并发处理的异步 ajax 调用

c# - 如何初始化AForge网络摄像头