0

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?

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>' + "&nbsp;" + '</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...

Dimitri
  • 95
  • 1
  • 7

2 Answers2

0

Does the problem occur when downloading or uploading? If on uploading you may increase maxRequestLength in web.config:

https://stackoverflow.com/a/9281987/2178028

Also, it can be occurs because of JSON length limit:

https://stackoverflow.com/a/20249635/2178028

Omer Faruk KAYA
  • 342
  • 2
  • 10
  • it's happening while retrieving data from data base. I'm doing upload, it works and then by executing function loadFileData(); it falls down to an error – Dimitri Aug 09 '20 at 11:22
  • download controller [HttpGet] public FileResult FileDownload(int? fileId) { FileDBEntities db = new FileDBEntities(); tblFile file = db.tblFiles.ToList().Find(p => p.id == fileId.Value); return File(file.Data, file.ContentType, file.Name); } – Dimitri Aug 09 '20 at 11:30
  • and Json: window.location = window.location.origin + '/File/FileDownload?fileId=' + id; – Dimitri Aug 09 '20 at 11:31
  • is not working... – Dimitri Aug 09 '20 at 11:35
  • Did you debug controller? In javascript side, it didn't give the exact error. Also you can try to disable response buffer: https://stackoverflow.com/questions/12710013/asp-net-mvc-returning-large-amounts-of-data-from-fileresult – Omer Faruk KAYA Aug 09 '20 at 11:39
  • But why in a download controller? the problem is when I'm retrieving data from database – Dimitri Aug 09 '20 at 11:45
  • the funny thing is if I replace a "big file with the small one, it keeps going normal " – Dimitri Aug 09 '20 at 11:47
0

this is how I've solved it:

public JsonResult FileIndex()
    {

        List<tblFile> fileList = new List<tblFile>();
        using (FileDBEntities db = new FileDBEntities())
        {
            fileList = db.tblFiles.ToList();
        }

        var jsonResult = Json(fileList, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
 }
Dimitri
  • 95
  • 1
  • 7