开发区图片上传到我们的服务器
This commit is contained in:
@@ -151,6 +151,8 @@
|
||||
<input id="imgUrl" name="imgUrl" type="text" style="width:500px "
|
||||
value="<#if developmentZone??>${developmentZone.imgUrl!}</#if>"/><br>
|
||||
</td>
|
||||
<#-- s-->
|
||||
<td> <input type="file" id="fileInput" accept="image/*"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
@@ -181,6 +183,45 @@
|
||||
</div>
|
||||
</div>
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<script>
|
||||
// 当文件选择变化时触发事件
|
||||
document.getElementById('fileInput').addEventListener('change', async () => {
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const file = fileInput.files[0];
|
||||
const token = "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB";
|
||||
|
||||
if (!file) {
|
||||
alert('请选择一个文件!');
|
||||
return;
|
||||
}
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('token', token); // 将 token 作为请求参数
|
||||
|
||||
try {
|
||||
// 发送 POST 请求上传文件
|
||||
const response = await fetch('http://open.fangjia.com/files/uploadGridFs', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (result.result.link) { // 假设返回值包含图片URL
|
||||
console.log(result.result.link);
|
||||
document.getElementById('imgUrl').value = result.result.link; // 将图片URL赋值给 imgUrl 输入框
|
||||
} else {
|
||||
alert('上传失败,未能获取图片URL');
|
||||
}
|
||||
|
||||
document.getElementById('response').innerText = '上传成功';
|
||||
|
||||
} catch (error) {
|
||||
console.error('上传失败:', error);
|
||||
document.getElementById('response').innerText = '上传失败,请查看控制台。';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
fetchProvinces();
|
||||
|
||||
Reference in New Issue
Block a user