gpt4 book ai didi

html - Input type = "file"自动丢失文件?

转载 作者:可可西里 更新时间:2023-11-01 13:32:27 25 4
gpt4 key购买 nike

我有两个文件类型的输入,一个在局部 View 中,另一个在主页中

局部 View

<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] 设置为文件对象。但是,如果我在控制台窗口中检查它,无论如何它总是显示?这是怎么回事?

问题

我需要提交表单,但是当我这样做时,图像(输入文件)作为空文件发送,但有时作为有效文件发送。正如我之前解释的,当我检查控制台中的值时,它是第一次显示,或者随机显示几次,然后突然间它就消失了,而我在窗口 (window[profilePic) 上设置的变量]) 总是有那个文件对象。
如果有人想知道,用户实际选择文件的原始/可见文件输入始终具有值。

最佳答案

出于安全原因,您不能这样做,所有通过 input type="file" 上传的文件都必须由用户手动完成。

但是,只要用户无论如何都会上传图片,您就应该在服务器端脚本中完成所有您想要的过程。

有关更多信息,请参阅此处的这篇文章:

How to set a value to a file input in HTML?

关于html - Input type = "file"自动丢失文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com