用ajax发送目标文件并发送[object object]

我有2种情况类似的表格。其中一个用于插入,另一个用于编辑数据。 在两次情况下,我使用一个函数来获取要发送的数据。 在插入形式的getData()函数中返回以下内容:

   {title: "test", category_id: "4", description: "test description", cover_file: "{"isowner":false,"mime":"image/jpeg","ts":15896352…/files/1/download.jpeg","path":"1/download.jpeg"}", …} category_id: "4" cover: "cover_file" cover_file: File(5022) {name: "download.jpeg", lastModified: 1589693668029, lastModifiedDate: Sun May 17 2020 10:04:28 GMT+0430 (Iran Daylight Time), webkitRelativePath: "", size: 5022, …} description: "test description"title: "test"__proto__: Object

但是以编辑形式在getData()中返回:

{title: "test title", category_id: "4", description: "test", cover_file: "[object Object]", id: 4}category_id: "4"cover: "editCover_file"cover_file: File(5022)lastModified: 1589692423454lastModifiedDate: Sun May 17 2020 09:43:43 GMT+0430 (Iran Daylight Time) {}name: "download.jpeg"size: 5022type: "image/jpeg"webkitRelativePath: ""__proto__: Filedescription: "test"id: 4title: "test title"__proto__: Object

我不知道为什么在编辑表单中,getData()函数在cover_file中返回[object object]并在controller中发送[object object]。所有情况都是相似的。

对于Elfinder图像数据,它的getData()函数的一部分有问题:

if ($("form#" + me.id + ' #' + key).hasClass('elfinder')) {
            let fileData = JSON.parse($("form#" + me.id + ' #' + key + '_file').val());
            let filename = fileData.name;

            new Promise(function (resolve, reject) {
                var request = new XMLHttpRequest();
                request.open('GET', fileData.url, true);
                request.responseType = 'blob';
                request.onload = function () {
                    var reader = new FileReader();
                    reader.readAsDataURL(request.response);
                    reader.onload = function (e) {

                        var base = e.target.result;
                        resolve(base);
                    };
                };
                request.send();

            }).then(function (dataurl) {
                var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n);
                }

                return new File([u8arr], filename, {type: mime});
            }).then(function (file) {
                data[map] = $("form#" + me.id + ' #' + key + '_file').prop('name');
                data[map + '_file'] = file;
            });

        }
评论