将大画布图像发送到服务器

时间:2016-09-20 04:57:06

标签: 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: '{ "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();
        }
    }
}

上传小图片时,上面的代码正常工作。如果我上传的图像超过2 MB。我收到以下错误。

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>

0 个答案:

没有答案