Saeid Mirzaei
Saeid Mirzaei

Reputation: 1236

Large file in Kendo UI Uploader

I want upload large file(600 MB) in ASP.Net Mvc with Kendo Uploader But I get flowing Exception:

OutOfMemoryException

Web.config: maxRequestLength and maxAllowedContentLength were set before too

maxRequestLength = "2097152" maxAllowedContentLength = "2147483647"

Upvotes: 1

Views: 4629

Answers (1)

Hamed Khatami
Hamed Khatami

Reputation: 545

Saeid is my coworker (really sharp man) and we solve the solution after a lot research, so we think share it with you....

first of all i want to describe solution. we want to upload large file asynchronous with kendo ui upload widget but we have a problem. when we upload large file (600MB or larger), application throw out of memory exception because application load 600MB to ram and .....

solution 1- if you want to use kendo ui uploader you must use following html code

<form method="post" enctype="multipart/form-data" action="api/UploadFile">
    <input name="files" id="files" type="file" />
</form>

$("#files").kendoUpload({
        async: {
            saveUrl: "/api/UploadFile",
            autoUpload: true
        },
        success: onSuccess,
        upload: onUpload
});

1-1:you must use enctype="multipart/form-data" for async upload, 1-2:action="api/UploadFile" i want to upload file to UploadFile web Api

if you want use html input file,please you below html code

<form method="post" enctype="multipart/form-data" action="api/UploadFile">
        <input name="files" id="files" type="file" />
</form>

2-Api must be has below code

public class UploadController : ApiController
    {
        public Task<HttpResponseMessage> PostFormData()
        {
            // Check if the request contains multipart/form-data.
            if (Request.Content.IsMimeMultipartContent() == false)
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);

            var task = Request.Content.ReadAsMultipartAsync(provider).
                ContinueWith<HttpResponseMessage>(t =>
                {
                    if (t.IsFaulted || t.IsCanceled)
                    {
                        Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                    }

                    foreach (MultipartFileData file in provider.FileData)
                    {
                        string directory = Path.GetDirectoryName(file.LocalFileName);
                        string filename = file.Headers.ContentDisposition.FileName.Replace(@"\","").Replace(@"""","");
                        File.Move(file.LocalFileName, Path.Combine(directory, filename));
                    }
                    return Request.CreateResponse(HttpStatusCode.OK);
                });

            return task;
        }
    }

2-1 : we determind app_data folder to save uploaded file

Now you can upload file Async but if you choose large file then you get out of memory exception

for resolve this problem ,you should say to mvc to not buffer data in UploadFile Api.

it has easy solution

Please Read following Article for solve it. Dealing with large file

Upvotes: 4

Related Questions