本帖最後由 calvinlai 於 2022-7-14 14:12 編輯

回覆 10# max918
因為每次填完form + attach an image file 後
click  "Add"  就gen 一行資料於最下方
再清空attach an image file> $('input[type="file"]')
直至 save 清空所有 form
所以 submit 唔可能 loop  $('input[type="file"]')  黎讀 file

咁我係唔係要應該save 前
再create另一隻 $('input[type="file"]')  黎塞返所有頁底  img blob url  入去當file 去submit ?

TOP

Option 1. 每次add image都整個新的input[type="file"], 跟手hide左舊的input[type="file"], upload 時再append去FormData.
Option 2. 將image 變成dataURL再放入JSON Object

TOP

本帖最後由 calvinlai 於 2022-7-15 12:24 編輯

回覆 12# katoru

謝謝指出方向!
現在都係嘗試用 option2 將image 變成dataURL再放入JSON Object
先由 file(imgfile)  轉成 base64 (image_input64 )

console 見 convert 倒
但係 reader.result 點 return 返 string   !



var image_input64 = null;
var imgfile = document.getElementById('fileElem').files[0];
if (imgfile) {
    img_bloburl= URL.createObjectURL(document.getElementById('fileElem').files[0]);

    image_input64 = convertFile(imgfile);
}

function convertFile(blob)
{
            var reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onloadend = function()
            {
                var base64data = reader.result;
               
                return base64data;
            }            
}

TOP

讀image係callback function, 佢唔會停響度等, 會直接完左個function
完左之後先割另外再入onloadend, 所以果度唔可以用黎return野, 應該響果度再call function run其他野

TOP

回覆 14# KinChungE


噢! 明白! THX!

TOP