【发布时间】:2014-12-28 04:06:03
【问题描述】:
我有两个文件类型的输入,一个在部分视图中,另一个在主页中
局部视图
<input type="file" name="image" id="image" onchange="readURL(this)"/>
在主页面
<input type="file" name="userProfilePic" id="userProfilePic" style="display:none" />
我想要的是,当用户在可见文件上传上更改 image/file 时,image/file 也应该在主/其他输入上更新。这是我的代码。
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#imagePreview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
// window['profilePic'] = input.files[0];
$('#userProfilePic').get(0).files[0] = input.files[0];
return false;
}
错误
这个错误很奇怪,当我打开我的控制台并检查文件时,它有时会出现,然后又不会。
在我的控制台窗口中
$('#userProfilePic').get(0).files[0]
File (file object details)
....
(after a second)
$('#userProfilePic').get(0).files[0]
undefined
而且这不是第一次发生。有时说,它显示了 5-6 次的值,然后第 7 次它不会...
$('#userProfilePic').get(0).files[0]
File (file object details)
....
(after a second)
$('#userProfilePic').get(0).files[0]
File (file object details)
....
(after a second)
$('#userProfilePic').get(0).files[0]
File (file object details)
....
(after a second)
$('#userProfilePic').get(0).files[0]
undefined
这就是我所有的代码,没有其他代码。现在,正如您在代码中看到的,我还将window[profilePic] 设置为文件对象。但是,如果我在控制台窗口中检查它,无论如何它总是显示?这是怎么回事?
问题
我需要提交表单,但是当我这样做时,图像(输入文件)被作为空文件发送,但有时作为有效文件发送。正如我之前解释的,当我检查控制台中的值时,它第一次显示,或者随机显示了几次,然后突然消失了,而我在窗口(@987654329@)上设置的变量总是有那个文件对象。
如果有人想知道,用户实际选择文件的原始/可见文件输入始终具有该值。
【问题讨论】:
-
我已经放置了一个更新的答案。
标签: html asp.net-mvc file input