具有使用常规格式将图像上传到服务器的功能。他们要求使功能Ctrl + V并加载图像。 我正在尝试,某些问题无法解决。
JS:
document.onpaste = function(event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
console.log(JSON.stringify(items));
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
var blob = item.getAsFile();
var reader = new FileReader();
reader.onload = function(event) {
document.getElementById('file-upload').files[0] = event.target.result;
document.getElementById('up-img').submit();
//alert(event.target.result);
};
reader.readAsDataURL(blob);
}
}
}
表单代码:
<form action="/upload-img.php" enctype="multipart/form-data" method="POST" id="up-img">
<label for="file-upload" class="custom-file-upload">
<i class="fa fa-plus"></i>
</label>
<input name="steamidpost" type="hidden" value="123">
<input name="img" accept="image/*" type="file" id="file-upload" onchange="this.form.submit()">
还有php代码/upload-img.php
<?php
include 'conn.php';
session_start();
if(isset($_POST['steamidpost'])) {
$id = $_POST['steamidpost'];
$img = $_FILES['img'];
if(isset($img['name'])) {
$filename = $img['tmp_name'];
$client_id = "MY SECRET ID";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID '.$client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close($curl);
$pms = json_decode($out, true);
$url = $pms['data']['link'];
if(isset($url)){
//add to mysql code
}
}
}
else {
exit;
}
?>
使用Ctrl + V
时,将触发代码(用于Alert完成的测试),但是图像不会加载,仅页面被更新。怎么了?
错误:
[Mon Oct 01 21:28:01 2018] [warn] [client #] mod_fcgid: stderr: PHP Warning: fopen(): Filename cannot be empty in ...
[Mon Oct 01 21:28:01 2018] [warn] [client #] mod_fcgid: stderr: PHP Warning: fread() expects parameter 1 to be resource, boolean given in /upload-img.php ...
答案 0 :(得分:0)
作品:
choices: Choice[];