te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - CopyClone files from File Input to another Input - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CopyClone files from File Input to another Input - Stack Overflow

programmeradmin5浏览0评论

If i open a file select, select a file, then click file select again and then click cancel, it forgets the originally selected file

//Main input
<input type="file" class="input" id="input" name="avatar">

//Backup input
<input type="file" class="input_two" id="input_two" name="avatar_two">

Is there a workaround for this, possibly creating a temporary input class and copying the .files over to this one so that i can still have access to the last file

$('.input').change(function () {
        var value = this.files;
        console.log(value);
        if(value.length > 0)
        {
            $(.avatar_two).assign(value)... //This is what i want to do
        }
        else
        {
            console.log("nada");
        }

is this possible?

If i open a file select, select a file, then click file select again and then click cancel, it forgets the originally selected file

//Main input
<input type="file" class="input" id="input" name="avatar">

//Backup input
<input type="file" class="input_two" id="input_two" name="avatar_two">

Is there a workaround for this, possibly creating a temporary input class and copying the .files over to this one so that i can still have access to the last file

$('.input').change(function () {
        var value = this.files;
        console.log(value);
        if(value.length > 0)
        {
            $(.avatar_two).assign(value)... //This is what i want to do
        }
        else
        {
            console.log("nada");
        }

is this possible?

Share Improve this question asked Feb 28, 2017 at 16:28 JeremyJeremy 1511 gold badge1 silver badge10 bronze badges 4
  • You can not set the value of an file input – epascarello Commented Feb 28, 2017 at 16:32
  • function copy() { var myObject, f; myObject = new ActiveXObject("Scripting.FileSystemObject"); f = myObject.file.copy("c:\\test.txt", "c:\\mytest.txt"); } – cvsr.sarma Commented Feb 28, 2017 at 16:34
  • 1 ActiveX? is it the 90s? – epascarello Commented Feb 28, 2017 at 16:44
  • "Uncaught ReferenceError: ActiveXObject is not defined" nice job at multi-browser patibility. – fdomn-m Commented Feb 28, 2017 at 16:50
Add a ment  | 

3 Answers 3

Reset to default 7

it is possible at 2021 via DataTransfer (at least latest chrome/firefox)

let file = document.getElementById("file");
let back = document.getElementById("back");

file.addEventListener('change', function() {
  let files = this.files;
  let dt = new DataTransfer();
  for(let i=0; i<files.length; i++) {
    let f = files[i];
    dt.items.add(
      new File(
        [f.slice(0, f.size, f.type)],
        f.name
    ));
  }
  back.files = dt.files;
});
<input type="file" id="file" multiple>
<input type="file" id="back" multiple>

It is not possible to set .files property of <input type="file"> element, which references read-only FileList object. See React/Javascript--FileReader/<input/>/adding image.

You can call File.prototype.slice() to create a copy of the File object. Or use FormData, FormData.prototype.append() to store selected files.

var clone, i = 0;
var fd = new FormData();

$('.input').change(function() {
    var value = this.files;
    console.log(value);
    if (value.length > 0) {
        clone = value[0].slice(0, value[0].size, value[0].type);
        fd.append("file-" + (i++) /* this.name */, value[0]);
    } else {
        console.log("nada");
    }
});

As I know, you cannot do that for security reasons, at least setting the value, I don’t know if coping or cloning it’s possible but take a look to this answer

发布评论

评论列表(0)

  1. 暂无评论