简体   繁体   中英

C# Web API - Out of memory when upload big image

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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