[英]add prefix to filename, javascript
I would like to add a prefix to the filename of a file, before I send it to the server, I have an input field like this: 我想在文件名中添加一个前缀,然后再将其发送到服务器之前,我有一个输入字段,如下所示:
<input type="file" id="uploadControlId">
And if I do this: 如果我这样做:
var element = document.getElementById("uploadControlId");
console.log('file: ' + element);
I get the output: 我得到的输出:
file: [object HTMLInputElement]
I can read the filename by splitting the element, but since the element is a url, I have no idea how I can change the name. 我可以通过拆分元素来读取文件名,但是由于元素是url,所以我不知道如何更改名称。
splitting: 分裂:
var element = document.getElementById("uploadControlId");
var elements = element.value.split("\\");
var fileName = parts[elements.length - 1];
You can not change the value of an file input, becase of security reasons. 出于安全原因,您不能更改文件输入的值。 You can do your own AJAX script to post a file with other name, but not using the standard browser/form functionality.
您可以执行自己的AJAX脚本来发布其他名称的文件,但不使用标准的浏览器/表单功能。 In your case there's a simple solution, just add an hidden field and pass the name trough this filed.
在您的情况下,有一个简单的解决方案,只需添加一个隐藏字段并通过该字段传递名称即可。 Value of this filed can be set on submit.
此字段的值可以在提交时设置。
<input id="uploadControlId" type="file" >
<input id="uploadFilename" type="hidden" value="">
there's a tag jquery, so I'll use jquery 有一个标签jquery,所以我将使用jquery
$('#yourFormId').submit(function(){
var filename = "prefix_" + $($("#uploadControlId").val().split("\\")).last();
$('#uploadFilename').val(filename);
return true;
});
or you can do something similar 或者你可以做类似的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.