提问人:Farhan Hermansyah 提问时间:10/31/2023 最后编辑:Farhan Hermansyah 更新时间:11/1/2023 访问量:52
每次我使用签名创建新用户时,我的签名文件都已损坏
My Signatures File Got Corrupted everytime i create new user with signature
问:
每次我创建带有签名的新用户时,我都有问题。文件大小为 0kb 并且无法打开,我尝试访问它工作但未显示的文件。
所以我有这个控制器来存储具有新签名的新用户。
public function store(Request $request)
{
// Save Signature from data URL to storage
$signatureData = $request->signature;
$fileName = time() . '.png';
$filePath = 'public/signatures/' . $fileName; // Save to storage/app/public
$imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $signatureData));
Storage::put($filePath, $imageData);
// Validasi dan penyimpanan data user
$user = User::create([
'name' => $request->name,
'user_code' => $request->user_code,
'password' => Hash::make($request->password),
'signature_path' => 'signatures/' . $fileName, // Save part of path todatabase
]);
// Add role(Jabatans) to user with pivot table
$user->jabatans()->attach($request->jabatan_id);
Session::flash('success', 'User Berhasil Dibuat');
return redirect()->route('user-create');
}
我得到了这个签名代码块
<div class="mb-3">
<label for="signature">Signature</label>
<canvas id="signature-pad" class="signature-pad" width=400 height=200 style="border:1px solid black;"></canvas>
<button type="button" id="clearSignature" class="btn btn-secondary mt-2">Clear
Signature</button>
</div>
这是我的 Javascript 使用签名板库
<script>
var canvas = document.getElementById('signature-pad');
var signaturePad = new SignaturePad(canvas);
// Function to check if the form is completely filled out
function isFormValid(formId) {
const form = document.getElementById(formId);
return form.checkValidity();
}
// Function to display SweetAlert confirmation
function showConfirmationAlert() {
if (isFormValid('userForm')) {
// Replace 'userForm' with your form's ID
Swal.fire({
title: 'Are you sure?',
text: 'Do you want to submit this form?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
}).then((result) => {
if (result.isConfirmed) {
// Submit the form
document.getElementById('userForm').submit();
// Replace 'userForm' with your form's ID
// Show success alert
Swal.fire(
'Success!',
'Your form has been submitted.',
'success'
);
}
});
} else {
Swal.fire(
'Error!',
'Please fill out all required fields.',
'error'
);
}
}
// Add an event listener to the clear signature button
document.getElementById('clearSignature').addEventListener('click', function() {
signaturePad.clear();
});
// Add an event listener to the submit button
document.getElementById('submitButton').addEventListener('click', function(e) {
e.preventDefault();
// Check the signature first
if (signaturePad.isEmpty()) {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Please provide your signature.',
});
return;
}
// Then, check for user code duplication
const userCode = document.getElementById('user_code').value;
fetch('/check-usercode', {
method: 'POST',
body: JSON.stringify({
user_code: userCode
}),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
})
.then(response => response.json())
.then(data => {
if (data.exists) {
// If the user code already exists, show a warning with SweetAlert
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'User code already exists!',
});
} else {
// If the user code is unique, display a form submission confirmation
showConfirmationAlert();
}
})
.catch(error => console.error('Error:', error));
});
</script>
*在我的边栏选项卡中编辑整个 JS
所以我尝试预览签名图像,但图像没有显示,我已经链接了我的存储/应用程序/公共/签名/。它起作用了,并且创建了文件,问题是文件总是无法打开或损坏。以前它有效,然后它没有:D
答:
0赞
Tipu Sultan Eiko
11/1/2023
#1
您没有将您的签名发布到控制器方法,
if 是您提到的控制器方法的路由check-usercode
像这样修改它
fetch('/check-usercode', {
method: 'POST',
body: JSON.stringify({
user_code: userCode,
name: name, // also noticed you have name in the controller
signature: canvas.toDataURL()
}),
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
})
评论
signature