【问题标题】:How to retrieve "big" file from database in MVC with AJAX如何使用 AJAX 从 MVC 中的数据库中检索“大”文件
【发布时间】:2020-08-09 11:09:18
【问题描述】:

将超过 0.5MB 的文件上传到 SQL 数据库后,我的 AJAX 调用出错: 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)})})(参数),i?o =a.length:n&&(s=t,l(n))}返回这个}

有人能告诉我我在这里做错了什么以及如何在 UI 中显示文件名吗?

我的 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);
    }
   });
 }

我的控制器:

[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);

    }

“小”文件没有错误。奇怪的是,使用普通剃须刀代码它可以正常工作......

【问题讨论】:

    标签: sql-server asp.net-mvc file-upload json.net asp.net-ajax


    【解决方案1】:

    下载或上传时是否出现问题?如果在上传时,您可以在 web.config 中增加 maxRequestLength:

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

    另外,也可能是因为 JSON 长度限制:

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

    【讨论】:

    • 从数据库中检索数据时发生。我正在上传,它可以工作,然后通过执行函数 loadFileData();它归结为一个错误
    • 下载控制器 [HttpGet] public FileResult FileDownload(int?fileId) { FileDBEntities db = new FileDBEntities(); tblFile 文件 = db.tblFiles.ToList().Find(p => p.id == fileId.Value);返回文件(文件。数据,文件。内容类型,文件。名称); }
    • 和Json:window.location = window.location.origin + '/File/FileDownload?fileId=' + id;
    • 不工作...
    • 你调试控制器了吗?在 javascript 方面,它没有给出确切的错误。您也可以尝试禁用响应缓冲区:stackoverflow.com/questions/12710013/…
    【解决方案2】:

    我就是这样解决的:

    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;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-16
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2017-05-18
      • 2015-09-15
      相关资源
      最近更新 更多