我有一個用戶上傳檔案(影像)的表單,一旦他們選擇檔案就會上傳檔案,我想要做的是獲取上傳檔案的 url 并將其預填充到下一個文本輸入值中。還能夠從第一個輸入(檔案名)重命名上傳的檔案
這是上傳php腳本:
<?php
header('Access-Control-Allow-Origin: *');
/* Get the name of the uploaded file */
$filename = $_FILES['file']['name'];
/* Choose where to save the uploaded file */
$location = "uploads/".$filename;
/* Save the uploaded file to the local filesystem */
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
echo 'Success';
} else {
echo 'Failure';
}
?>
這是表單腳本
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>upload</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
<script>
async function uploadFile() {
let formData = new FormData();
formData.append("file", fileupload.files[0]);
await fetch('assets/php/upload.php', {
method: "POST",
body: formData
});
alert("url location");
}
</script>
<label>File Name</label><br>
<input type="text" name="filename"><br>
<input id="fileupload" type="file" name="fileupload" onchange="uploadFile()"/><br>
<input type="text" value="url of the uploaded file"/>
<script src="" async defer></script>
</body>
有關問題的更多資訊:結果將是兩個表單,一個會將檔案上傳到本地服務器并將 url 回傳到第二個表單,第二個表單會將資訊發送到要提交的谷歌表單,這是用戶的一種解決方法無需登錄就無法直接上傳到谷歌表單,這是我找不到解決方案的實際問題,所以我試圖這樣做作為將檔案直接上傳到谷歌表單的解決方法
非常感謝你的幫助
uj5u.com熱心網友回復:
感謝您的幫助,我想通了。
這是其他人正在尋找的答案
上傳.php檔案
<?php
/* Get the name of the uploaded file */
$filename = $_FILES['file']['name'];
$ipad = rand(1256252, 25665588665);;
$newfilename = round(microtime(true)) . '.' .$ipad . '.' .$filename;
$location = "uploads/".$newfilename;
/* Save the uploaded file to the local filesystem */
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
echo $location;
} else {
echo 'Failure';
}
?>
這是js腳本:
<script>
async function uploadFile() {
let formData = new FormData();
formData.append("file", fileupload.files[0]);
var response = await fetch('assets/php/upload.php', {
method: "POST",
body: formData
});
let data = "localhost/assets/php/" await response.text()
console.log(data);
outputfile.value = data
}
</script>
<input id="fileupload" type="file" class="upload" name="fileupload" value="new" onchange="uploadFile()"/>
<br>
<input type="text" id='outputfile'/>
雖然我無法從文本輸入中更改檔案名,但我創建了一個隨機名稱
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/510396.html
上一篇:HTML表單動作相對路徑錯誤
