添加文件(JS对象)到输入type =" file" [英] Add a File (JS object) to input type="file"

查看:104
本文介绍了添加文件(JS对象)到输入type =" file"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法将File对象添加到文件输入字段。

I need a way to add a File object to a file input field.

请注意我不想设置文件字段的值 。 (这是一个安全风险,......我知道)

我已经拥有文件本身(及其所有内容!)

Note that I do not want to set the value of the file field. (which is a security risk, ... I know that)
I already have the file itself (and all its contents!)

我正在创建这样的File对象:

I'm creating the File object like this:

canvas.toBlob(function(blob) {
  var file = new File([blob], 'file.png');
  // and now I want to add MY File to the input here
});

请不要告诉我如何上传文件,我知道XMLHttpRequest Level 2.我想要将文件上传到外部网站(可能使用用户脚本或类似的东西)。

Please don't tell me about how to upload the File, I know about XMLHttpRequest Level 2. I want to upload the File to an external website (potentially using a userscript or something similar).

那么,我该如何实现呢?
或者如果不可能:为什么我不能添加我实际拥有的文件(因为我在浏览器中创建它(作为虚拟文件,在用户文件系统中甚至不存在))到输入我也拥有的领域? (从技术上讲,输入字段可能是我自己的,这在这里无所谓)

So, how can I achieve that? Or if it's not possible: why can't I add a File that I actually own (because I created it in the Browser (as a virtual File, which does not even exist in the users filesystem)) to an input field that I own too? (technically the input field could be my own, that does not matter here)

推荐答案

不,你不能。

取自这里


您似乎想从drop事件中获取File对象并将其分配给元素。不幸的是,你做不到。只有用户才能选择文件;您无法动态更改将上传的文件,因为出于安全考虑,浏览器会拒绝JavaScript此功能。

It seems like you want to take the File object from the drop event and assign it to the element. Unfortunately, you can't do that. Only the user can select files; you can't dynamically change the files which will be uploaded because browsers deny JavaScript this ability for security reasons.

这篇关于添加文件(JS对象)到输入type =" file"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆