C# 将大型画布图像发送到服务器
我上传了一个大图像,并对其进行裁剪,然后将裁剪后的画布转换为base64。我使用ajax将base64数据传递给服务器C# 将大型画布图像发送到服务器,c#,ajax,asp.net-mvc,canvas,base64,C#,Ajax,Asp.net Mvc,Canvas,Base64,我上传了一个大图像,并对其进行裁剪,然后将裁剪后的画布转换为base64。我使用ajax将base64数据传递给服务器 var pic = result.toDataURL("image/png"); pic = pic.replace(/^data:image\/(png|jpg);base64,/, "") $.ajax({ type: 'POST', url: Sitepath + '/PhotoContest/UploadPhoto', data: '{ "Ima
var pic = result.toDataURL("image/png");
pic = pic.replace(/^data:image\/(png|jpg);base64,/, "")
$.ajax({
type: 'POST',
url: Sitepath + '/PhotoContest/UploadPhoto',
data: '{ "ImageData" : "' + pic + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {
}
});
在C#MVC中,我编写了如下操作
[WebMethod()]
public void UploadPhoto(string ImageData)
{
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
string FileName = "Photo_" + unixTimestamp+".jpg";
var path = Path.Combine(Server.MapPath("~/Content/Uploads"), FileName);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(ImageData);
bw.Write(data);
bw.Close();
}
}
}
上面的代码在我上传小图片时起作用。如果我上传的图像超过2MB。我得到下面的错误
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
虽然我已经在web.config中写了下面三个
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"/>
</webServices>
</scripting>
</system.web.extensions>
<system.web>
<httpRuntime maxRequestLength="1048576" /> <!--1GB-->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" /> <!--1GB-->
</requestFiltering>
</security>
</system.webServer>
不确定这是否会修复它,但是maxAllowedContentLength
是一个uint
,因此它的最大值是4294967295。(传入的值远高于此值。)错误似乎超出了它的范围。不确定这是否可以修复它,但maxAllowedContentLength
是一个uint
,因此它的最大值是4294967295。(传递给它的值远远高于此值。)错误似乎超出了它的范围。