0

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:

user3321095
  • 185
  • 1
  • 10

1 Answers1

1

You have to give write permissions for the IIS User Group to the folder you're trying to upload the file ConfigurationManager.AppSettings["UploadImagePath"]

llouk
  • 513
  • 4
  • 15
  • I gave modify permission to Network Service. That should do it, right? – user3321095 Oct 18 '16 at 21:10
  • IIS_IUSRS is the what you need to give the permissions to – Simon Price Oct 18 '16 at 21:14
  • Thanks but that didn't make a difference – user3321095 Oct 18 '16 at 21:54
  • image.Save(path + fileName, jpegCodec, encoderParams); This line throws the exception right? Check those out: http://stackoverflow.com/questions/20009791/how-do-i-grant-network-service-read-permissions , http://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permissions – llouk Oct 18 '16 at 22:25
  • Network Service already has modify permissions... see the first comment above in this answer. – user3321095 Oct 18 '16 at 22:40
  • Adding APPPOOL\AppPoolName with modify permissions to the upload folder did not work either. Thanks though. – user3321095 Oct 18 '16 at 23:38
  • I added static back to the web method because I had been getting a 500 internal server error. After all the changes I'm still getting the 500 error. – user3321095 Oct 18 '16 at 23:47