我試圖在請求後同時上傳多個圖像,並在同一頁面上顯示它們,然後(多次上傳後)我可以選擇要保留的圖片。我用這裏找到的所有可能的答案(How to upload multiple files using PHP, jQuery and AJAX,
Uploading Multiple Files using AJAX and PHP),但沒有成功。上傳多張圖片,TokenMismatch錯誤,雖然我確實在表單中有令牌。有任何想法嗎?
我不斷收到TokenMismatchException
500(內部服務器錯誤)+ TokenMismatchException在VerifyCsrfToken.php線68:
任何想法?
這裏是我的代碼:
HTML:
<form id="uploadForm" enctype="multipart/form-data">
<div class="container upload-picture-device">
<div class="col-md-2 col-sm-1 col-xs-12"></div>
<div class="col-md-4 col-sm-5 col-xs-12">
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-de-pe btn-file">
Choose <input id="fileInput" type="file" name="files[]" class="pics" multiple>
</span>
</span>
<input type="text" class="form-control input-upload" readonly>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
</div>
</div>
<div class="col-md-4 col-sm-5 col-xs-12 text-left">
<button id="uploadPhotos" type="submit" class="btn-continua">Upload picture</button>
</div>
</div>
</form>
的Javascript:
$('#uploadForm').submit(function (e) {
e.preventDefault();
var form = new FormData();
var files = document.getElementsByClassName('pics');
for (var i=0; i<files.length; i++) {
form.append("files[pic" + i + "]", files[i][0]); // add receipt to form
}
form.append('action', 'upload-photos'); // specify action
$.ajax({
url: '{{url("/photos/device")}}',
type: 'POST',
data: form,
cache: false,
processData: false,
contentType: false,
success:function(data) {
console.log(data);
},
error: function(xhr, desc, err) {
// I have some error handling logic here
}
});
});
後錯誤信息! – BKF
您正在執行AJAX請求並且未發送csrf令牌。 –
VerifyCsrfToken.php中的TokenMismatchException第68行:+500內部服務器錯誤 –