【发布时间】:2014-09-24 12:11:50
【问题描述】:
在我的页面上,我使用 HTML5 在浏览器上上传文件。该文件是 PDF,必须在 <input type="text"/> 中显示它
这是我的代码:
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
for (var i = 0, f; f = files[i]; i++) {
console.log(f.name);
console.log(f.type);
console.log(f.size);
//Update document info
$("#document_filename").find("input").val(f.name);
$("#document_mimetype").find("input").val(f.type);
var reader = new FileReader();
// Closure to capture the file information.
reader.onloadend = (function(theFile) {
return function(e) {
content = e.target.result.replace(/^data:[^;]*;base64,/, "");
console.log(content.length);
$("#document_content").find("input").val(content);
a = document.getElementById("document_content").getElementsByTagName("input")[0];
a.value = content;
console.log(a.value.length);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
当我上传一个 1.5MB 的大文件时,我的控制台显示:
contrat.pdf // The file name
application/pdf // The file type
1510788 // The file size
2014384 // The length of the b64 content
524288 // The length of string once inserted in the input box??
有人可以向我解释我在截断这些数据方面做错了什么吗?
谢谢,
【问题讨论】:
-
首先要确保
document.getElementById("document_content").getElementsByTagName("input")[0]返回与$("#document_content").find("input").val(content)相同的元素。你为什么要这样做?其次,您对<input>有任何验证吗?maxlength? -
嗨,它返回的是相同的。我这样做是因为我正在编写 Jquery,并且当我输入框被截断时,我已经在本机 js 中编写了这一行以避免 jquery 错误。而且我仍然有同样的行为......
-
HTML 代码丢失。
标签: javascript html input truncate