0

I have problem in C# when upload big image with Json string contains base64 by WebAPI

Code JS:

function uploadImagePlace() {
    return new Promise((resolve, reject) => {
        var photo = $('#thumbnail-preview').attr('src');
        var isPhotoDB = $('#thumbnail-preview').attr('data-photodb');
        if (photo && isPhotoDB == 'false') {
            var formData = new FormData();
            formData.append("Photo", ($('#thumbnail-preview').attr('src') == '') ? "" : $('#thumbnail-preview').attr('src'));
            $.ajax({
                url: serverFile + "UploadImagePlace/AddImage",
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (result) {
                    resolve({ error: false, data: result });
                },
                error: function () {
                    reject({ error: true, data: 'Error upload' });
                }
            });
        } else {
            resolve({ error: false, data: null });
        }
    });
}

Code C#

var httpRequest = HttpContext.Current.Request;
var ListPhoto = httpRequest["Photo"].ToString();
var lstPlaceImg = JsonConvert.DeserializeObject<List<PhotoUploadViewModel>>(ListPhoto);

Error in catch Exception:

exception of type 'system.outofmemoryexception' was thrown. Ex.Source: Newtonsoft.Json

I don't know how to fix this. Can any one help me?

1
  • Can you define PhotoUploadViewModel? Commented Jan 5, 2018 at 9:30

1 Answer 1

1

Try by adding these lines to your Config file inside configuration tag.

<system.web>
<httpRuntime targetFramework="4.<Your Version>" maxRequestLength="<Max file size you want to send>" />
</system.web>

It worked for me! Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it but not ok. <httpRuntime targetFramework="4.5.1" requestValidationMode="2.0" requestPathInvalidCharacters="" relaxedUrlToFileSystemMapping="true" maxRequestLength="1073741824" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.