简体   繁体   English

将base64从javascript传递到C#服务器函数

[英]Pass base64 from javascript to C# Server function

I want to save an image on a server by sending its properties via ajax.. In the JS I'm encoding the image in base64 before I send it (along with other parameters i need): 我想通过ajax发送其属性来将图像保存在服务器上。在JS中,在发送图像之前(我需要其他参数)我在base64中对图像进行编码:

    $.ajax({
            type: 'POST',
            url: 'PhotoSelect.aspx/GetData',
            data: JSON.stringify({ action: "save", x: x, y: y, width: width, height: height, type: type, obj_id: obj_id, image: image, team: teamVal, jersey: jerseyVal, spot: spotVal }),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json'
})

And i receive the request on server side by: 我通过以下方式在服务器端收到请求:

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public static JsonReturn GetData(string action, string x, string y, string width, string height, string type, string obj_id, string image, string team, string jersey, string spot)
{
//Process Image
}

Now the weird part is that this works (by this i mean it reaches the GetData function) for some images! 现在,怪异的部分是,对于某些图像,这是可行的(通过这个,我是说它到达了GetData函数)! When i attempt to send a smaller (or lower quality) image this works. 当我尝试发送较小(或较低质量)的图像时,此方法有效。 When i attempt to send images that have really long base64 strings it doesnt reach the function.. 当我尝试发送具有非常长的base64字符串的图像时,它没有达到该功能。

Can anyone think of anything that might be wrong with this? 有人能想到这有什么问题吗?

Thank you guys for your suggestions... for any others that might look for a solution: 谢谢您的建议...对于可能寻求解决方案的其他建议:

You have to increase the maxJSONLength by adding this in your web.config 您必须通过在web.config中添加maxJSONLength来增加它

<system.web.extensions>
   <scripting>
     <webServices>
        <jsonSerialization maxJsonLength="50000000"/> <!-- Set the maximum request size  (the value is in KB here) -->
     </webServices>
   </scripting>
</system.web.extensions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM