如何将上传文件的名称设置为输入 type:text on javascript in html
How to set uploaded file's name to an input type:text on javascript in html
我想将文件上传控件添加到我的 html 页面,如下所示。
<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server"
data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'."
data-val-maxlength-max="600" value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>
我无法做到的是在用户浏览文件后捕获所选文件,并通过javascript或jquery将其设置为rptScreenShot的值。任何帮助将不胜感激。
像下面这样的东西?
$("#upld_sample1").on("change", function(e){
console.log("Fileinfo:", e.target.files[0])
$("#rptScreenShot").val(e.target.files[0].name)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server" data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'." data-val-maxlength-max="600"
value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>
我想将文件上传控件添加到我的 html 页面,如下所示。
<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server"
data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'."
data-val-maxlength-max="600" value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>
我无法做到的是在用户浏览文件后捕获所选文件,并通过javascript或jquery将其设置为rptScreenShot的值。任何帮助将不胜感激。
像下面这样的东西?
$("#upld_sample1").on("change", function(e){
console.log("Fileinfo:", e.target.files[0])
$("#rptScreenShot").val(e.target.files[0].name)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server" data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'." data-val-maxlength-max="600"
value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>