iT邦幫忙

0

前端用 append 製造多個 input與 base64,後端接每個 base64,但都只有一張會被保存?

這是我前端:

$('body').on('change', '.add-btn',function(e){
    e.preventDefault();

    var id = $('.admin-upload-inline').length + 1;
    var removeBtn = $('<i class="admin-upload-remove material-icons">highlight_off</i>');
    var iconLayout = $('<div class="admin-upload-inline add-btn pointer" id="icon-layout-'+id+'">');
    var fileInput = $(
      '<input type="hidden" id="file'+id+'" name="icon[]" class="admin-upload" value="">'
    + '<img src="../images/30.png" id="pre'+id+'" class="admin-upload-review">');

    removeBtn.click(function() {
			$(this).fadeOut().parent().remove();
			var wrappers = $(".admin-upload-inline");
			if (wrappers.length > 1) {
				var newId = 1;
				wrappers.each(function(){
					renewId = "icon-layout-" + newId;
					$(this).attr("id",renewId);
					newId++;
				});
			}
		});

    if(id < 8){
      iconLayout.append(fileInput);
  		iconLayout.append(removeBtn);
  		$(".addend-display").append(iconLayout);
    }

     var reader = new FileReader();
     reader.onload = function(e) {
       $('#pre' + id).attr('src', e.target.result);
       $('#file' + id).attr('value', e.target.result);
     }

     reader.readAsDataURL(this.files[0]);
<div class="admin-upload-inline pointer">
        <input type="file" id="file1" name="icons[]" class="add-btn admin-upload" accept="image/jpeg, image/png, image/jpg">
        <img src="../images/30.png" id="pre1" class="admin-upload-review">
        <i class="material-icons upload_cover_icon admin-upload-icon-add">add_circle_outline</i>
      </div>

      <div class="addend-display">

      </div>

我的後端這樣接:

foreach ($_POST['icon'] as $value) {
      preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result);
        $file_ext = '.'.$result[2];
        file_put_contents('../../images/product/'.md5().$file_ext, base64_decode(str_replace($result[1], '', $value)));
    }

為什麼他都只會是最後一張圖會保存到?
假設我上傳ABC圖好了
他永遠只會儲存到 C圖? (../../images/product/)
於是我試著印印看icon欄位,我發現我同時上傳ABC圖,直接印出 $_POST['icon] 他好像他只會有一張圖?(一個base64的連結)
怎麼會這樣?

看更多先前的討論...收起先前的討論...
呃…妳的前端、後端都空白?
Homura iT邦高手 1 級 ‧ 2018-07-10 11:13:40 檢舉
巴哈姆特的圖庫不能外連啊...
改用imgur吧!
froce iT邦大師 1 級 ‧ 2018-07-10 11:21:36 檢舉
後端看post只有一張圖的話,問題就在前端,所以先看前端寫的code吧。
火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 11:26:42 檢舉
好我用用看 imgur
沒有圖的情況下,先提供你自己找問題的方法,
前端送出前先印出來看看,後端接到後也印出來看看,
你就知道是哪個環節出了問題
Homura iT邦高手 1 級 ‧ 2018-07-10 11:31:33 檢舉
你右鍵複製圖片位址可以直接得到圖片網址
能把code貼上來嗎? 不要貼圖片~
火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 11:47:17 檢舉
好了
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
最佳解答

妳的問題在於 FileReader 只處理了一張上傳圖片,多張的處理請參考以下方式:
https://blog.csdn.net/wjy199506/article/details/62046093

/images/emoticon/emoticon28.gif

看更多先前的回應...收起先前的回應...
火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 11:52:40 檢舉

但是我有用谷歌檢查看,確實每一個 input 都有不同的 base網址噎

火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 11:53:39 檢舉

靠杯,我現在看才發現雖然預覽不一樣,但是base是一樣的。。。。

女生不要隨便靠杯~
/images/emoticon/emoticon06.gif

火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 12:05:46 檢舉

但是我只有一個 input file 而已,這怎麼辦?

火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 12:12:05 檢舉

我現在有點亂,我發現我傳了四張圖,一跟四是一樣的 base,二跟三世一樣的 base....

火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 12:16:06 檢舉

先問一個問題好了,怎麼樣在幾乎同一個時間,產生不同的字串?

foreach ($_POST['icon'] as $value) {
      preg_match('/^(data:\s*image\/(\w+);base64,)/', $value, $result);
        $file_ext = '.'.$result[2];
        file_put_contents('../../images/product/'.md5(time()).$file_ext, base64_decode(str_replace($result[1], '', $value)));
    }

我用 time() 發現沒用,根本同一個時間處理完成

只有一個 input file,那就是妳js寫錯的關係~

火爆浪子 iT邦研究生 1 級 ‧ 2018-07-10 12:27:24 檢舉

我目的就是只要一個 input file就好了,他產生 base64 到 input hidden去

froce iT邦大師 1 級 ‧ 2018-07-10 12:38:06 檢舉

把產生的base64,push到array裡,就好了啊。
剩下用for去做。

froce iT邦大師 1 級 ‧ 2018-07-10 12:54:24 檢舉

不過 $('body').on('change')...
你是認真的嗎?綁定的不會太多?

只有一個 input file,應該要加上multiple="multiple"的屬性才能多檔上傳吧~

froce iT邦大師 1 級 ‧ 2018-07-10 13:40:41 檢舉

他應該是要一個input重複用吧。
所以才用base64去接。轉base64後就是字串了,跟input files應該脫離關係了。

不過其實我懶得看他的程式碼,只是用猜的就是了。

我要發表回答

立即登入回答