简体   繁体   English

将文件名从文件上传传递到文本字段

[英]Pass filename from file upload to text field

I have a part of a form where a user can upload a file. 我有一个表单的一部分,用户可以上传文件。 I want only the filename to be sent to a text field in the same form. 我只想将文件名发送到同一表单的文本字段。 So if user uploaded "C:/Folder/image.jpg", the text field should show "image.jpg". 因此,如果用户上传了“C:/Folder/image.jpg”,则文本字段应显示“image.jpg”。 I tried some code myself but I know it's wrong: 我自己尝试了一些代码,但我知道这是错误的:

function ff_uploadimages_action(element, action)
{var m = data.match(/((*):\/)/(.*)[\/\\]([^\/\\]+\.\w+)$/);
switch (action) {
case 'change':
if (data.match(/((*):\/)/(.*)[\/\\]([^\/\\]+\.\w+)$/).value)
ff_getElementByName('filename').value = m[2].text;
        default:;
    } // switch
} // ff_uploadimages_action

ff_uploadimages is the field to upload file, and filename is the textfield where name should appear. ff_uploadimages是要上载文件的字段,filename是应显示名称的文本字段。 Any help at all is appreciated! 任何帮助都表示赞赏! Thanks. 谢谢。

Here's one way to do it 这是一种方法

 document.getElementById('upload').onchange = uploadOnChange; function uploadOnChange() { var filename = this.value; var lastIndex = filename.lastIndexOf("\\\\"); if (lastIndex >= 0) { filename = filename.substring(lastIndex + 1); } document.getElementById('filename').value = filename; } 
 <input id="upload" type="file" /> <input id="filename" type="text" /> 


you don't mention jQuery but given it's popularity here's the same solution using jQuery 你没有提到jQuery,但考虑到它的受欢迎程度,这是使用jQuery的相同解决方案

jQuery: jQuery的:

$('#upload').change(function() {
    var filename = $(this).val();
    var lastIndex = filename.lastIndexOf("\\");
    if (lastIndex >= 0) {
        filename = filename.substring(lastIndex + 1);
    }
    $('#filename').val(filename);
});

Demo: 演示:

http://jsfiddle.net/pxfunc/WWNnV/4/ http://jsfiddle.net/pxfunc/WWNnV/4/

HTML: HTML:

<input id="upload" type="file" onChange="uploadOnChange(this)" />
<input id="filename" type="text" />

JS: JS:

function uploadOnChange(e) {
    var filename = e.value;var lastIndex = filename.lastIndexOf("\\");
    if (lastIndex >= 0) {
        filename = filename.substring(lastIndex +1);
    }
    document.getElementById('filename').value = filename;
}

A shorter way in jQuery would be the following: jQuery中的一个较短的方法是:

HTML HTML

<input type="file" id="inputFile" class="hidden"/>
<input type="text" id="inputDisplayFileName" readonly/>
<button id="buttonChooseFile">Choose file</button>

jQuery jQuery的

$("#buttonChooseFile").click(funtion()({
    $("#inputFile").click();        
});

$("#inputFile").change(function(){
    var fileName = $("#inputFile").prop('files')[0]["name"];

    $("inputDisplayFileName").val(fileName);
});

In this example the default file upload is hidden so that you can style the 'upload file input' as desired. 在此示例中,隐藏了默认文件上载,以便您可以根据需要设置“上载文件输入”的样式。 Clicking the button will trigger the original (hidden) file upload. 单击该按钮将触发原始(隐藏)文件上载。 After choosing the file the .onchange() will do the rest of the work, copying the file the 'read only input text'. 选择文件后, .onchange()将完成剩下的工作,将文件复制为“只读输入文本”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM