
获取图片上传后的路径
用户通过 <input type="file"> 选择图片后,前端可能需要获取其路径。默认情况下,<input type="file"> 只会提供文件的名称。
解决方案
可以使用 javascript 获取文件的绝对路径。具体步骤如下:
立即学习“Java免费学习笔记(深入)”;
- 在表单中使用 enctype="multipart/form-data" 属性。
- 为 <input type="file"> 添加 onchange 事件处理程序,将文件的路径赋值给一个隐藏的输入框(例如 <input type="hidden" id="thefilepath">)。
- 在后端代码中,可以从隐藏输入框获取上传文件的绝对路径。
代码示例
html:
<form name="thisform" method="post" action="action_url" id="thisform" enctype="multipart/form-data">
<input type="file" name="thefile" onchange="document.getelementbyid('thefilepath').value=this.value">
<input type="hidden" id="thefilepath" name="thefilepath" value="">
</form>action 代码:
String filePath = request.getParameter("theFilePath");通过这种方式,前端即可获取图片上传后的绝对路径。

















