我有一个包含各种字段和一些文件输入的 HTML 表单。 问题是,网络服务器在windows机器上运行,一些用户从mac os或linux上传文件,其中包含windwos不允许的字符,例如::和? .
获取文件的文件处理程序 (Axon.Ivy/JAVA) 无法处理这些字符并抛出异常。我无法更改文件处理程序。
由于原始文件名无关紧要,我的最佳解决方案是重命名文件,或者在实际 POST 发生之前操作 POST 数据中的文件名属性。最好使用 javascript。
有什么办法可以实现吗? 提前致谢!
最佳答案
是的,用FormData API ,您可以在通过 AJAX 发送之前为您的文件设置其他名称:
...
var form = new FormData();
form.append(someField, someInput.value);
// when appending a File or a Blob, the third param is the name of the File
form.append('fileField', fileinput.files[0], filename);
xhr.send(form);
sub.onclick = e => {
var form = new FormData();
form.append('fileField', inp.files[0], 'myFile.ext');
console.log(form.get('fileField'));
}
<input type="file" id="inp">
<button id="sub">submit</button>
关于javascript - 是否有可能在发布之前重命名 html 文件输入的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276582/