Having a difficult time figuring this one out. I'd really appreciate some help.
Works fine on localhost and single test server. Production environment is a web cluster.
Removing the base64 image from data array in the ajax call and as a parameter in the web method allows everything to work fine.
Here's my code:
JS
var img = $('.finalize-img').attr('src'); //Src is a base64 string
//var img = base64.replace(/^data:image\/(png|jpg);base64,/, "");
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'Thank-You.aspx/Upload',
dataType: "json",
processData: false,
data: "{'img':'" + img + "', 'firstname':'" + firstname + "', 'lastname':'" + lastname + "', 'emailaddress':'" + emailaddress + "'}",
success: function (msg) {
var m = msg.d;
},
error: function (jqXHR, error, errorThrown) {
if (jqXHR.status && jqXHR.status == 400) {
alert('An error occurred. Please try again.');
//alert(jqXHR.status + " -- " + jqXHR.responseText);
}
}
});
C#
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Upload(string img, string firstname, string lastname, string emailaddress)
{
string base64 = img.Replace("data:image/png;base64,", "");
string path = ConfigurationManager.AppSettings["UploadImagePath"];
string fileName = "Image_" + DateTime.UtcNow.Year + DateTime.UtcNow.Month + DateTime.UtcNow.Day + DateTime.UtcNow.Hour + DateTime.UtcNow.Minute + DateTime.UtcNow.Second + DateTime.UtcNow.Millisecond + "_" + Guid.NewGuid() + ".jpg";
int imageQuality;
if (!int.TryParse(ConfigurationManager.AppSettings["UploadImageQuality"], out imageQuality))
{
imageQuality = 50;
}
byte[] bytes = Convert.FromBase64String(img);
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
}
try
{
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, imageQuality);
ImageCodecInfo jpegCodec = null;
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < codecs.Length; i++)
{
if (codecs[i].MimeType == "image/jpeg")
{
jpegCodec = codecs[i];
}
}
if (jpegCodec != null)
{
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
image.Save(path + fileName, jpegCodec, encoderParams);
}
}
catch (Exception ex)
{
return "Message:" + ex.Message + " Source:" + ex.Source + " Inner Exception:" + ex.InnerException;
}
return fileName;
}
*********UPDATE********** Found this in the event viewer Event code: 3005 Event message: An unhandled exception has occurred. Event time: 10/18/2016 3:32:57 PM Event time (UTC): 10/18/2016 8:32:57 PM Event ID: 6a016eb99ac74d558cb1ec42df643299 Event sequence: 13221 Event occurrence: 12 Event detail code: 0
Application information: Application domain: /LM/W3SVC/2/ROOT-5-131212864443437500 Trust level: Full Application Virtual Path: / Application Path: D:\Websites\MS\ Machine name: MOVWEB4
Process information: Process ID: 4048 Process name: w3wp.exe Account name: NT AUTHORITY\NETWORK SERVICE
Exception information: Exception type: ArgumentException Exception message: Unknown web method Upload. Parameter name: methodName at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Request information:
Request URL: http://beta.ms.com/Thank-You.aspx/Upload
Request path: /Thank-You.aspx/Upload
User host address:
User: Anonymous
Is authenticated: False
Authentication Type:
Thread account name: NT AUTHORITY\NETWORK SERVICE
Thread information: Thread ID: 27 Thread account name: NT AUTHORITY\NETWORK SERVICE Is impersonating: False Stack trace: at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Custom event details: