Click here to Skip to main content
14,665,104 members
Rate this:
Please Sign up or sign in to vote.
After uploading file more that 0,5MB into SQL database, my AJAX call falls into error:
function(){if(a){var t=a.length;(function r(t){v.each(t,function(t,n){var i=v.type(n);i==="function"?(!e.unique||!c.has(n))&&a.push(n):n&&n.length&&i!=="string"&&r(n)})})(arguments),i?o=a.length:n&&(s=t,l(n))}return this}

can somebody tell me what I'm doing wrong here and how to show name of the file in UI?

What I have tried:

My Ajax:

function loadFileData() {

    $.ajax({
        type: "GET",
        url: "/File/FileIndex",
        dataType: "JSON",
        success: function (data) {
            $.each(data, function (i, val) {
                var trow = $('<tr/>').data("id", val.id);
                var trowa = $('<tr/>');
                var trowb = $('<tr/>').data("id", val.id);
                trow.append('<td colspan="2"><a href="#" class="FileDownload">' + val.Name + '</a>' + " " + '</td>');
                trowa.append('<td><input type="file" id="FileUpload" /></form></td>');
                trowb.append('<td><input type="button" class="btnUpload" value="Upload File" /><input type="button" id="btnClear" value="Clear" /></td>');

                tab.append(trow);
                tab.append(trowa);
                tab.append(trowb);

            });
            $("#showFiles").html(tab);
        },
        error: function (err) {
            alert("Failed! Please try again." + err.error);
        }
       });
     }


My Controller:

[HttpPost]
        public JsonResult UpdateJsionFile(int? id, HttpPostedFileBase file)
        {
            byte[] bytes;
            //decimal fileSize = 100;
            var supportedTypes = new[] { "txt","doc","docx","pdf", "xls", "xlsx", "png" };
            var fileExt = System.IO.Path.GetExtension(file.FileName).ToLower().Substring(1);
            using (BinaryReader br = new BinaryReader(file.InputStream))
            {
                bytes = br.ReadBytes(file.ContentLength);
            }

            if(!supportedTypes.Contains(fileExt))
            {
                return Json(new { success = false, error = "File extention is invalid - upload only WORD/PDF/EXCEL/TXT/PNG files" }, JsonRequestBehavior.AllowGet);
            }


            if(file.FileName.Length>50 )
            {
                return Json(new { success = false, error = "File name is too long, max. 50 symbols" }, JsonRequestBehavior.AllowGet);
            }

            if (file.ContentLength > 4096)
            {
                return Json(new { success = false, error = "File size is too big, max 10MB" }, JsonRequestBehavior.AllowGet);
            }


            using (FileDBEntities db = new FileDBEntities())
            {
                tblFile f = db.tblFiles.Where(p => p.id == id).FirstOrDefault();

                f.Name = Path.GetFileName(file.FileName);
                f.ContentType = file.ContentType;
                f.Data = bytes;

                db.SaveChanges();
            }
            return Json(new { success = true }, JsonRequestBehavior.AllowGet);

        }



there is no error with "small" files. Strange is, that with normal razor code it works fine... error according to a xhr.status is "500" but why it works then with small files???
Posted
Updated 11-Aug-20 8:35am
v3
Comments
Patrice T 9-Aug-20 7:00am
   
and the error message is ?
Member 14803832 9-Aug-20 7:01am
   
function(){if(a){var t=a.length;(function r(t){v.each(t,function(t,n){var i=v.type(n);i==="function"?(!e.unique||!c.has(n))&&a.push(n):n&&n.length&&i!=="string"&&r(n)})})(arguments),i?o=a.length:n&&(s=t,l(n))}return this}
Patrice T 9-Aug-20 7:06am
   
this don't look like an error message.
Member 14803832 9-Aug-20 7:08am
   
Yes it is, this is the message what I'm recieving, when ajax-call is executed
Member 14803832 9-Aug-20 7:09am
   
error: function (err) {
alert("Failed! Please try again." + err.error);
}
F-ES Sitecore 9-Aug-20 10:17am
   
No, that's the request object. The error function has three parameters and the actual error is in the ones you're not using. Consult the documentation;

https://api.jquery.com/jquery.ajax/
Member 14803832 9-Aug-20 10:27am
   
jqXHR , textStatus, errorThrown are returning to me the same text:

function(){if(a){var t=a.length;(function r(t){v.each(t,function(t,n){var i=v.type(n);i==="function"?(!e.unique||!c.has(n))&&a.push(n):n&&n.length&&i!=="string"&&r(n)})})(arguments),i?o=a.length:n&&(s=t,l(n))}return this}
Member 14803832 11-Aug-20 14:29pm
   
error is "500", server side, but why it works then with small files?
Patrice T 11-Aug-20 14:33pm
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard Deeming 12-Aug-20 10:47am
   
Check the application event log on the server to see what the actual error message is.

You might be hitting the request limits.
Request Limits <requestLimits> | Microsoft Docs[^]
Member 14803832 12-Aug-20 12:09pm
   
Hi Richard,how to do it?
Member 14803832 12-Aug-20 13:08pm
   
Console shows me this line as an Error:
xhr.send((options.hasContent && options.data) || null);
Member 14803832 12-Aug-20 13:21pm
   
Message is "failed to load recource, server responced with status 500 ()"
Richard Deeming 13-Aug-20 4:14am
   
"Status 500" just means something went wrong on the server. Check the application event log on the server to get the real error details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100