当我尝试将文件上传到服务器时,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/