javascript - 自动上传文件
问题描述
我创建了一个带有 html 和 javascript 的网站,可以访问您的网络摄像头,当用户按下拍照按钮时,一张照片就会被捕获。我将如何直接将其上传到存储它的某个云文件夹?
谢谢
解决方案
在按钮所在的页面上,:
- 创建拍摄照片的按钮(我不知道这是如何完成的)
<button>Snap photo!</button>
- 创建一个具有任何值的表单(不可见的表单)......任何东西都可以是值,只要确保它不为空
- 使用javascript,以便在单击按钮时将表单提交到php页面
将表单的目标设置为您创建的 iframe,这样当他们拍摄照片时,它会指向同一页面,而无需重新加载页面] 这是我刚才所说的代码:
折断
</form>
<script>
函数 rad(){ document.getElementById("废话").submit();
</div>
使用以下代码创建文件处理程序:
$errors = []; // Store all foreseen and unforseen errors here $fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions $fileName = $_FILES['myfile']['name']; $fileSize = $_FILES['myfile']['size']; $fileTmpName = $_FILES['myfile']['tmp_name']; $fileType = $_FILES['myfile']['type']; $fileExtension = strtolower(end(explode('.',$fileName))); $uploadPath = $currentDir . $uploadDirectory . basename($fileName); if (isset($_POST['submit'])) { if (! in_array($fileExtension,$fileExtensions)) { $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file"; } if ($fileSize > 1000000000) { $errors[] = "This file is more than 1GB. Sorry, it has to be less than or equal to 1GB"; } if (empty($errors)) { $didUpload = move_uploaded_file($fileTmpName, $uploadPath); if ($didUpload) { echo basename($fileName) ; } else { echo "An error occurred somewhere. Try again or contact the admin"; } } else { foreach ($errors as $error) { echo $error . "These are the errors" . "\n"; } } }
?>
唯一的问题是,这会将图像上传到您的服务器而不是“云文件夹”......但它是一个文件夹,只需将云文件夹放在您的网络服务器中,然后在$uploadDirectory = "/uploads/";
ie 中指定云文件夹的名称,将“上传”更改为您的云文件夹的名称....
希望这会有所帮助
推荐阅读
- java - 如何在java中的坐标数组中找到左上角坐标
- c# - Instantiate the next item when the first item is dropped
- python - My python code that converts numbers between bases has several errors. What could be wrong and how can I find them?
- python - Python - Pivot and create histograms from Pandas column, with missing values
- neo4j - 删除属性之一并在满足条件下将其创建为新节点
- python - Django initialize data test for all test classes
- python-3.x - 在 Pandas 中读取 CSV 并以所需格式获取输出
- primefaces - 向javascript函数发送参数
- c++ - 进程退出,值为 3221225477
- flutter - Using nearest neighbour interpolation for flutter image